C++: nested class of a template class

后端 未结 4 2079
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 13:09

Consider the following code:

template < typename T >
struct A
{
    struct B { };
};

template < typename T >
void f( typename A::B ) {          


        
4条回答
  •  自闭症患者
    2020-12-10 13:50

    Following up on the question in the "Update", here is a situation in which the call to f would be ambiguous (if it were allowed, that is):

    // Definitions of generic "struct A", as well as "f()", are the same as above
    
    // But additionally, consider a specialized "struct A", defined as follows:
    
    template <>
    struct A
    {
        typedef A::B B;
    }
    
    // Now consider the call to "f", similarly to before:
    
    int main()
    {
        // Possibility 1 for argument to "f()"
        // A::B x;
    
        // Possibility 2 for argument to "f()": Use the specialized version of "struct A"
        A::B x;
    
        f(x); // which value to deduce for type T?  Could be "int" or "double"
    }
    

    Notice the ambiguous pair of potential instantiated functions f: Both f() and f would result in a successfull call to f().

提交回复
热议问题