我想從 c 20 概念開始。
class MyClass
{
template<typename T>
void copy(const T& data);
};
copy()僅在Tis時有效is_trivially_copyable。在 C 20 之前我會使用
static_assert(is_trivially_copyable<T>, "Type must be trivially copyable");
函式內copy。
但據我了解,這是一個可以使用概念的問題。經過一番谷歌搜索后,我想出了
template <typename T>
concept isTriviallyCopyable = std::is_trivially_copyable_v<T>;
但是,將其添加到函式時
class MyClass
{
template<isTriviallyCopyable>
void copy(const isTriviallyCopyable & data);
};
這給了我一個編譯器錯誤。你能幫幫我嗎?
uj5u.com熱心網友回復:
您需要為sTriviallyCopyable要應用的型別添加一個引數。那會給你
class MyClass
{
template<isTriviallyCopyable T>
void copy(const T & data);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515147.html
上一篇:函式模板在需要呼叫時不考慮
下一篇:使用函式回傳型別的類模板引數推導
