Le code suivant est compilé avec gcc (g ++) mais se plaint de clang (C++).clang (C++) impossible de trouver le nom dans la spécialisation
j'attendais la ligne n1::generic(*it);
pour trouver un my_traits<bool>
(ou my_traits<const bool>
) mais la spécialisation, il semble chercher le nom specific
dans le vector
(const?) À partir de cette même spécialisation.
De plus, ceci est spécifique à bool. D'autres types comme int fonctionnent bien. (J'ai essayé d'ajouter une spécialisation my_traits<vector<bool> >
(et aussi const bool) mais cela n'a pas aidé).
#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.
Ceci est dû malheureux 'std :: vecteur' spécialisation –
Justin