Wou*_*jen 8 c++ templates c++-concepts
我有两个(或更多)模板,每个模板都可以调整一组特定的类,这些类由一个概念标识.为了使两个模板具有相同的名称,它们必须是特化.
template< typename T >
struct pin_in { static_assert( always_false<T>::value, . . . ); };
template< is_pin_in T >
struct pin_in< T > . . .
template< is_pin_in_out T >
struct pin_in< T > . . .
Run Code Online (Sandbox Code Playgroud)
当其中一个专业化匹配时,这可以正常工作.如果没有匹配,则选择基本模板,并且我得到断言失败.该机制有效.我喜欢概念!
但我收到的错误消息(GCC 7.2.0)指向断言.我可以以某种方式使基本模板不被选中,所以我会得到一条错误消息,告诉没有模板匹配参数类?
bol*_*lov 10
万岁,我找到了解决方案!您需要的是限制主模板:
template <class T>
requires is_pin_in<T> || is_pin_in_out<T>
struct pin_in {};
template <is_pin_in T>
struct pin_in<T> {};
template <is_pin_in_out T>
struct pin_in<T> {};
Run Code Online (Sandbox Code Playgroud)
您会收到一条很好的诊断消息:
Run Code Online (Sandbox Code Playgroud)<source>: In function 'auto test()': 29 : <source>:29:16: error: template constraint failure pin_in<char> a; ^ 29 : <source>:29:16: note: constraints not satisfied 7 : <source>:7:24: note: within 'template<class T> concept const bool is_pin_in<T> [with T = char]' constexpr concept bool is_pin_in = std::is_same_v<T, int>; ^~~~~~~~~ 7 : <source>:7:24: note: 'std::is_same_v' evaluated to false 9 : <source>:9:24: note: within 'template<class T> concept const bool is_pin_in_out<T> [with T = char]' constexpr concept bool is_pin_in_out = std::is_same_v<T, unsigned>; ^~~~~~~~~~~~~ 9 : <source>:9:24: note: 'std::is_same_v' evaluated to false Compiler exited with result code 1
好吧,我的信息有一些虚拟的约束,但你明白了
| 归档时间: |
|
| 查看次数: |
484 次 |
| 最近记录: |