C++ 使用概念启用类模板的成员函数
所以我有一个概念C++ 使用概念启用类模板的成员函数,c++,c++17,c++-concepts,c++20,C++,C++17,C++ Concepts,C++20,所以我有一个概念Fooable: template <typename T> concept bool Fooable() { return requires(...){ ... }; } template <typename T> class Bar { public: template // ??? requires Fooable<T> void MemFun(); }; 在具有概念TS的C++17或C++2a
Fooable
:
template <typename T>
concept bool Fooable()
{
return requires(...){ ... };
}
template <typename T>
class Bar
{
public:
template // ???
requires Fooable<T>
void MemFun();
};
在具有概念TS的C++17或C++2a中是否可能?在概念TS和C++20设计中,函数都有一个可选的Required子句。因此,不需要将成员函数设置为模板来约束它:
void MemFun() requires Fooable<T>;
void MemFun()需要可食性;
约束可以在函数后面的尾随位置:
template <typename T>
class Bar
{
public:
void MemFun() requires Fooable<T>;
};
模板
分类栏
{
公众:
void MemFun()需要可食性;
};
答案并非如此,但如果您实际上不需要禁用的函数来不参与重载解析,您可以简单地使用
static\u assert
@HolyBlackCat在测试中执行完整模板实例化,以查看所有成员函数是否正确。添加static_assert会破坏其中的一些。使用概念TS,可以通过void MemFun()要求Fooable(){}