C++ 引用类型的别名模板在SFINAE上下文中作为模板参数传递
我在G++6.1.0(C++ 引用类型的别名模板在SFINAE上下文中作为模板参数传递,c++,templates,c++14,sfinae,C++,Templates,C++14,Sfinae,我在G++6.1.0(-std=c++14开关)中遇到了以下问题,我不明白编译器为什么拒绝代码 我有一个helper struct格式良好,它在替换另一个提供的类型时检查提供的模板参数是否格式良好: 模板 结构形式良好:std::false\u类型{}; 模板 结构形式良好:std::true\u类型{}; 我想检查类型是否可引用。所以我的想法是写以下内容: /(#1) 模板 使用引用_t=t&; 静态断言(!是格式良好的::值,“引用到void!?”; 但我得到一个编译器错误: main.
-std=c++14
开关)中遇到了以下问题,我不明白编译器为什么拒绝代码
我有一个helper struct格式良好
,它在替换另一个提供的类型时检查提供的模板参数是否格式良好:
模板
结构形式良好:std::false\u类型{};
模板
结构形式良好:std::true\u类型{};
我想检查类型是否可引用。所以我的想法是写以下内容:
/(#1)
模板
使用引用_t=t&;
静态断言(!是格式良好的::值,“引用到void!?”;
但我得到一个编译器错误:
main.cpp: In instantiation of 'struct is_well_formed<reference_t, double>':
main.cpp:62:51: required from here
main.cpp:54:20: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class R, class T, class> struct is_well_formed'
: std::false_type {};
^
main.cpp:54:20: note: expected a class template, got 'reference_t'
main.cpp:54:20: error: type/value mismatch at argument 1 in template parameter list for 'is_well_formed<R, T, <template-parameter-1-3> >::is_well_formed'
main.cpp:54:20: note: expected a class template, got 'reference_t'
此外,以下作品确实让我感到困惑:
这可能是一个编译器错误,用户在看到这篇文章后在GCC Bugzilla上报告了它。用户建议 而不是显而易见的
template <class...>
using void_t = void;
模板
使用void\u t=void;
这使得原始帖子中的选项
(#2)
无法工作。很有趣。只有当reference\u t
生成一个引用类型时,这似乎才失败。报告为@t.C。嗯,这很快:D如果这真的是一个bug,而且之前没有人遇到/报告过它,那真的很奇怪-这看起来不像是最奇特的代码…@t.C.如果你不介意写一个bug,我们可以用一个指定它是bug的答案来结束它吗?还可以使用decltype(std::declval())
(这是参考
的正确定义,与无效
相反)。
// (#3)
template<class T>
using pointer_t = T*;
static_assert(is_well_formed<pointer_t, void>::value, "No pointer to void!?");
template<class T>
using reference = decltype(std::declval<T&>());
template <class...>
struct make_void { using type = void; };
template <typename... T>
using void_t = typename make_void<T...>::type;
template <class...>
using void_t = void;