partial specialization with dependent name (typename)

前端 未结 2 1735
渐次进展
渐次进展 2021-01-25 11:23

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

相关标签:
2条回答
  • 2021-01-25 11:49

    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"?
    
    0 讨论(0)
  • 2021-01-25 11:49

    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.

    0 讨论(0)
提交回复
热议问题