2016-11-09 14 views
2

カスタムトレイトとbool型のC++ 11列挙型を使用する場合は、次のコードは、グラムで罰金コンパイル++と打ち鳴らすと(私がテストしたすべてのバージョンを)失敗:コンパイルエラー

#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; } 

エラーは(これだけで!)

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); 

私はこれが十分小さい例だと思います。私はintにBOOLから列挙型を変更すると

- エラーが消える:ここではそれのためのオンラインのコンパイラへのリンクです。

なぜこのようなことが起こっていますか?これはもともと、doctestCatchのテストフレームワークを使用して発見されました。ここにはCatchのbug reportがあります。それはclangバグでしょうか?

答えて

2

私はあなたの質問に答えることはできませんが、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); 
       ^
... 
+0

多分私達が打ち鳴らすためにバグを提出しようとする必要があります - 誰もが持ってアカウント? – onqtam

+1

バグ報告 - [こちら](https://llvm.org/bugs/show_bug.cgi?id=31229) – onqtam