2017-09-18 5 views
0

次のコードはgcc(g ++)でコンパイルしますが、clang(C++)で文句を言います。clang(C++)が特殊化内で名前を見つけることができません

私は専門my_traits<bool>(またはmy_traits<const bool>)を探すために行n1::generic(*it);を期待していたけど、それはvector内の名前specificを探しているようです(定数?)同じ専門分野の中から。

また、これはboolに固有です。 intのような他の型はうまく動作します。 (私はmy_traits<vector<bool> >(およびconst bool)の特殊化を追加しようとしましたが、それは役に立たなかった)。

#include <vector> 

namespace n1 { 

    template <class T> struct my_traits { }; 

    template <> struct my_traits<bool> { 
     static void specific(bool b) { } 
    }; 

    template <> struct my_traits<int> { 
     static void specific(int b) { } 
    }; 

    template <typename T> void generic(const T& t) 
    { 
     my_traits<T>::specific(t); 
    } 

    template <typename T> struct my_traits<std::vector<T> > { 
     static void specific(const std::vector<T>& b) 
     { 
      if (! b.empty()) { 
       for (typename std::vector<T>::const_iterator it = b.begin(); 
         it != b.end(); ++it) { 
        n1::generic(*it); 
       } 
      } 
     } 
    }; 
} 

namespace n2 { 
    struct ArrayOfBoolean { 
     std::vector<bool> values; 
    }; 

    struct ArrayOfInt { 
     std::vector<int> values; 
    }; 
} 

namespace n1 { 

    template<> struct my_traits<n2::ArrayOfBoolean> { 
     static void specific(const n2::ArrayOfBoolean& v) { 
      n1::generic(v.values); 
     } 
    }; 

    template<> struct my_traits<n2::ArrayOfInt> { 
     static void specific(const n2::ArrayOfInt& v) { 
      n1::generic(v.values); 
     } 
    }; 
} 

c++  codec.cc -o codec 
In file included from codec.cc:1:./codec.h:17:23: error: no member named 'specific' in 'n1::my_traits<std::__1::__bit_const_reference<std::__1::vector<bool,std::__1::allocator<bool> > > >' 
    my_traits<T>::specific(t); 
    ~~~~~~~~~~~~~~^ 
./codec.h:26:25: note: in instantiation of function template specialization 'n1::generic<std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here 
    n1::generic(*it); 
     ^
./codec.h:17:23: note: in instantiation of member function 'n1::my_traits<std::__1::vector<bool, std::__1::allocator<bool> > >::specific' requested here 
    my_traits<T>::specific(t); 
       ^
./codec.h:47:17: note: in instantiation of function template specialization 'n1::generic<std::__1::vector<bool, std::__1::allocator<bool> > >' requested here 
    n1::generic(v.values); 
     ^
1 error generated. 
+2

これは、不幸な 'std :: vector '専門化 – Justin

答えて

2

奇妙それはそうと、vector<bool>はどのbool Sを保持していません。あなたが本当の参照を得ることができないビットだけ。

したがって、n1::generic(*it);では、const bool&はなく、__bit_const_referenceプロキシクラスのみです。

+0

https://isocpp.org/blog/2012/11/on-vectorboolが良い記事ベクトルであるためです。 –

関連する問題