替换失败有时是一个错误
Substitution failure is sometimes an error?
下面的代码片段尝试实现一个'std::is_constructible<a,>':
#include <type_traits>
struct A {
// A(int);
};
template< typename T >
struct cstr_int
{
static int mi;
template< typename C >
static typename std::enable_if<
std::is_same< decltype( T( mi ) ), T >::value, char >::type choose( C * );
static long choose( ... );
enum { value = ( sizeof( decltype( choose( & mi ) ) ) == 1 ) };
};
bool const b1 = cstr_int< A >::value;
使用g++这工作得很好;使用clang++打印以下错误:
tmp/sfinae04.cc:14:29: error: no matching conversion for functional-style cast from 'int' to 'A'
std::is_same< decltype( T( mi ) ), T >::value, char >::type choose( C * );
^~~~~
tmp/sfinae04.cc:20:17: note: in instantiation of template class 'cstr_int<A>' requested here
bool const b1 = cstr_int< A >::value;
^
tmp/sfinae04.cc:3:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const A' for 1st argument
struct A {
^
tmp/sfinae04.cc:3:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'A' for 1st argument
struct A {
^
tmp/sfinae04.cc:3:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
1 error generated.
版本信息:clang Version 3.3 (trunk 172418) (llvm/trunk 172417), g++ (Debian 4.7.2-4) 4.7.2.
我的问题:恕我直言,这应该是一个SFINAE。我在这里错过了什么,还是clang++有问题?
(我知道有is_constructible(),但我不允许使用它-这不是我的问题的重点。)
虽然它看起来像是替换失败,但它实际上是在模板实例化期间检测到的失败,因此是可诊断的错误。
不是一个SFINAE上下文,因为enable_if
不依赖于被推导的模板参数。
如此:
template< typename T >
struct cstr_int
{
static int mi;
template< typename C >
static typename std::enable_if<
std::is_same< decltype( C( mi ) ), C >::value, char >::type choose( C * );
static long choose( ... );
enum { value = ( sizeof( decltype( choose( (T*)0 ) ) ) == 1 ) };
};
相关文章:
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- 在Cython中使用C库时,我遇到了一个错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 我试图用c++编写递归fibonacci序列,但当我编译时,我遇到了一个错误
- 从system()调用G++会返回一个错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 使用声明:GCC 和 Clang 的另一个错误?
- 全球免费给出一个错误.调试器不解释
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- 为什么第三板有一个错误
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 我需要在 Android Studio 中构建 NDK. 但它返回一个错误