Problem with functions accepting inner classes of template classes

后端 未结 3 2043
遥遥无期
遥遥无期 2020-12-11 19:51

I have a problem with inner classes in class templates. I have a template class (say: Matrix), and a subclass (say: Matrix::Row).

相关标签:
3条回答
  • 2020-12-11 20:33

    It would need to deduce the type T for the call to template function x, and template argument deduction is only allowed in a specific set of circumstances:

    http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/language_ref/template_argument_deduction.htm

    A<T>::B
    

    does not seem to be one of them :/

    0 讨论(0)
  • 2020-12-11 20:46

    How should the compiler be able to deduce this? Imagine the following setup:

    struct A { typedef int T; };
    struct B { typedef int T; };
    
    template <typename S> void foo(typename S::T);
    

    Now when you say int x; foo(x);, there's no way to match this unambiguously.

    The point is that you are not deducing a template parameter from a given class template, but rather just an arbitrary, free-standing type. The fact that that type was defined inside another class is not relevant for that.

    0 讨论(0)
  • 2020-12-11 20:47

    That is non-deducible context. That is why the template argument cannot be deduced by the compiler.

    Just imagine, you might have specialized A as follows:

    template <>
    struct A<SomeType>
    {
        typedef std::map <double, double> B;
    };
    

    Now this specialization has a nested type called B which is a typedef of std::map<double,double>.

    So how would the compiler deduce the type SomeType, given that A<SomeType>::B is std::map<double, double>?

    And in fact, there can be many such specializations, as such:

    template <>
    struct A<SomeOtherType>
    {
        typedef std::map <double, double> B;
    };
    

    Even this specialization has B as nested type.

    Now if I say A<T>::B is std::map<double,double>, then can you say what T is? Is it SomeType? or SomeOtherType?

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