c – 使用概念启用类模板的成员函数

前端之家收集整理的这篇文章主要介绍了c – 使用概念启用类模板的成员函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有一个概念Fooable:
template <typename T>
concept bool Fooable()
{
    return requires(...){ ... };
}

我有一个类模板Bar,它将类型T作为模板参数,我想只在T为Fooable时才启用成员函数

template <typename T>
class Bar
{
public:
    template // ???
        requires Fooable<T>
    void MemFun();
};

在概念TS或C 2a的C 17中是否可能?

解决方法

在概念TS和C 20设计中,函数都有一个可选的尾随require子句.因此,您不需要使您的成员函数成为约束它的模板:
void MemFun() requires Fooable<T>;

猜你在找的C&C++相关文章