使用自定义特征和bool类型的c ++ 11枚举时clang的编译错误

昂克坦

以下代码在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);

我相信这是一个很小的例子。这是它的在线编译器的链接:

当我将枚举类型从bool更改为int时,错误消失了。

那么为什么会这样呢?这最初是在使用doctestCatch测试框架时发现的-这是Catch错误报告可能是叮当响的虫子吗?

佩德罗·波切特(Pedro Boechat)

我知道它不能回答您的问题,但是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);
                   ^
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章