C 概念和模板专业化;如何获得用户友好的编译器错误
C++ concepts and template specialization; how to get a user-friendly compiler error
我有两个(或多个)模板,它们每个模板都可以调整一组由概念标识的特定类。为了使两个模板具有相同的名称,它们必须是专业。
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 > . . .
当专业之一匹配时,这可以正常工作。当不匹配基本模板时,我会得到断言失败。该机制起作用。我喜欢概念!
但是,我收到的错误消息(GCC 7.2.0)指向断言。我可以以某种方式不选择基本模板,因此我会收到一条错误消息,该消息告诉没有模板与参数类匹配?
欢呼,我找到了解决方案!您需要的是将主要模板限制为限制:
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> {};
您会收到一条很好的诊断消息:
<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
好吧,我的消息有一些虚拟约束,但是您得到了点
尝试使用std :: enable_if从过载分辨率中删除基本模板。这样的东西:
template< typename T >
struct pin_in<typename std::enable_if<false>::type> {};
template< is_pin_in T >
struct pin_in<typename std::enable_if<true>::type>< T > . . .
template< is_pin_in_out T >
struct pin_in<typename std::enable_if<true>::type>< T > . . .
相关文章:
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 我无法在某个点(从用户那里)获得输入,即使没有错误弹出编译
- 从用户那里获得无限量的输入,而不是打印最大的3个,而不使用数组
- 子类ConstexPR类以获得不同的用户定义的转换行为
- C 从没有STL的用户输入中获得CSVWITH的最大值
- C 概念和模板专业化;如何获得用户友好的编译器错误
- 如何存储我从使用 getch(); 获得的用户输入
- 在获得用户的2个输入后,需要以非常具体的方式创建乘法表
- 如何从用户那里获得输入字符串而不是示例,然后计算空格、标点符号、数字和字母.C++
- 我想让我的控制台更加丰富多彩.如何在用户输入时使用户输入(通过getline获得)
- 如何获得字符串和int的用户输入
- 如何获得Windows用户帐户的用户权利和特权
- 如何在C++中获得插入整个用户输入的脚本
- 为什么当我们从用户那里获得输入时我们需要一个缓冲区
- 如何每秒刷新终端并获得用户输入
- IM 创建一个 C++ 程序,其中用户可以尝试 2 次尝试密码用户名组合.如果他们无法获得它,他们的程序就会停止
- C++类和对象使用set方法获得用户输入
- BeagleBone Black从用户空间获得中断