当使用自定义trait和bool类型的c++11枚举时,clang的编译错误
Compilation errors with clang when using a custom trait and a c++11 enum of type bool
下面的代码在g++下可以很好地编译,但在clang(我测试过的所有版本)下无法编译:
#include <iostream>
namespace has_insertion_operator_impl
{
typedef char no;
typedef char yes[2];
struct any_t
{
template <typename T>
any_t(const T&);
};
yes& testStreamable(std::ostream&);
no testStreamable(no);
no operator<<(const std::ostream&, const any_t&);
template <typename T>
struct has_insertion_operator
{
static std::ostream& s;
static const T& t;
static const bool value = sizeof(testStreamable(s << t)) == sizeof(yes);
};
} // namespace has_insertion_operator_impl
template <typename T>
struct has_insertion_operator : has_insertion_operator_impl::has_insertion_operator<T>
{};
enum A : bool {
Yup = true,
Nop = false,
};
template <typename T>
bool getTraitVal(const T&) { return has_insertion_operator<T>::value; }
int main() { std::cout << getTraitVal(A::Yup) << std::endl; }
错误(只有clang !)如下:
prog.cc:24:59: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'const A')
static const bool value = sizeof(testStreamable(s << t)) == sizeof(yes);
我相信这是一个足够小的例子。以下是它的在线编译器链接:
- 叮当声3.8
- g + + 6.1
当我将枚举类型从bool改为int时,错误消失。
为什么会这样呢?这个问题最初是在使用doctest和Catch测试框架时发现的——下面是Catch的bug报告。会不会是一只叮当响的虫子?
我知道它不回答你的问题,但它似乎clang有一个问题与基础类型'bool'的枚举。
我把你的例子进一步简化为:
#include <iostream>
enum A : bool {
Yup = true,
Nop = false,
};
int main() {
A t = Yup;
std::cout << t;
}
在这里你已经对正在发生的事情有了一个感觉:
prog.cc:10:15: error: use of overloaded operator '<<' is ambiguous (with operand types 'ostream' (aka 'basic_ostream<char>') and 'A')
std::cout << t;
~~~~~~~~~ ^ ~
/usr/local/libcxx-3.8/include/c++/v1/ostream:195:20: note: candidate function
basic_ostream& operator<<(bool __n);
^
/usr/local/libcxx-3.8/include/c++/v1/ostream:198:20: note: candidate function
basic_ostream& operator<<(int __n);
^
...
相关文章:
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 连续枚举 C++11
- C ++枚举范围无法使用-std=c ++ 98进行编译,但使用-std=c ++ 11可以
- 安全使用严格枚举 C++ 11
- C++11 类中放置的枚举类的重载运算符
- 初始化枚举类 (C++11) 类型的二维 std::数组
- C++11 枚举前向导致"underlying type mismatch"
- 枚举布尔模板参数在C 11中
- 我可以创建一个可以接受任何枚举的模板(非类型)param类吗?C 11
- 使用 C++11 可变参数模板初始化枚举到字符串映射
- C++11 枚举类:加上 int 编译失败,为什么
- C++11 枚举类命名空间块
- 传递 C++11 枚举类作为模板,同时自动推断其类型
- C 11枚举类实例化
- 类型安全的c++11枚举类标志的模板
- 在C++11中分离枚举声明和定义
- 是否可以在C++11中指定枚举的位宽
- 如何在C++11中输出枚举类的值
- 如何在 C++11 中“使用”弱枚举
- C++11 在开关大小写中混合枚举类和无符号 int 将无法编译