问题
I have the following simple strinToTypeImpl
function which converts any kind of string into the template type. The problem I am concerned about is the fact that the compiler tells me for the partial specialization for typename MyMatrix<T>::Vector3
:
template parameter T not used in partial specialization
Can't I use dependent names in the specialization?
namespace details
{
template<typename T>
struct stringToTypeImpl{
bool operator()(T& t, const std::string& s)
{
std::istringstream iss(s);
return !(iss >> t).fail();
}
};
template<typename T>
struct stringToTypeImpl< typename MyMatrix<T>::Vector3 >{
// Replacing typename MyMatrix<T>::Vector3 by
// Eigen::Matrix<T,3,1> WORKS but why?
bool operator()(typename MyMatrix<PREC>::Vector3 & t, const std::string& s)
{
stringToVector3<PREC>(t,s);
}
};
}
回答1:
This is just another form of the problem that has been discussed many times: There's no one-to-one mapping from types X
to types T
such that MyMatrix<T>::Vector3 == X
.
Simple example:
MyMatrix<double> { typedef Vector3 int; };
MyMatrix<float> { typedef Vector3 int; };
stringToTypeImpl<int> // Help, what is "T"?
回答2:
Kerreck's answer and my comment there explain the problem. There is no way for the type system to map a member to a parent-of-member, so the ::
operator stops the deduction process which needs to match T
.
The simple way to a solution would be to bring Vector3
outside and specialize it on the matrix type, then make the member MyMatrix< T >::Vector3
a typedef.
template< typename Matrix >
struct Vector3 {
typename Matrix::ValueType x, y, z;
};
template< typename T >
struct MyMatrix {
typedef Vector3< MyMatrix > Vector3;
};
The partial specialization of StringToTypeImpl
needs to be on the final template type, not the typedef. Partial specialization cannot match across a set of typedefs, although specialization can match type to the type aliased by a single typedef name.
来源:https://stackoverflow.com/questions/18823564/partial-specialization-with-dependent-name-typename