簡體   English   中英

C++20 概念 在模板化上應用約束 function

[英]C++20 Concepts Apply constraint on templated function

我想從 c++20 概念開始。

class MyClass
{
  template<typename T>
  void copy(const T& data);
};

copy()僅在T is_trivially_copyable 在 C++20 之前我會使用

static_assert(is_trivially_copyable<T>, "Type must be trivially copyable");

copy function 中。

但是以我的理解,這是一個可以使用概念的問題。 經過一番谷歌搜索后,我想出了

template <typename T>
concept isTriviallyCopyable = std::is_trivially_copyable_v<T>;

但是,將其添加到 function 時

class MyClass
{
  template<isTriviallyCopyable>
  void copy(const isTriviallyCopyable & data);
};

這給了我一個編譯器錯誤。 你能幫我嗎?

您需要為要應用的sTriviallyCopyable添加一個類型參數。 那會給你

class MyClass
{
    template<isTriviallyCopyable T>
    void copy(const T & data);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM