Difference when omitting the C++ template argument list

前端 未结 1 1446
抹茶落季
抹茶落季 2021-01-19 14:41

When can you omit the C++ template argument list? For example in Visual Studio 2010 this piece of code compiles fine:

template
Vec2 V         


        
1条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 15:09

    Inside a class you can omit the argument on the class type:

    template
    struct A {
       A foo1; // legal
       A foo2; // also legal and identical to A foo
       A bar(A x) {...} // same as A bar(A x) {...}
    };
    

    Outside of a class scope you need the template arguments:

    // legal
    template
    A foo(A x) { return A(); }
    
    // illegal!
    template
    A foo(A x) { return A(); }
    

    If you declare a member function outside the class, you need the template list for the return type and for the class:

    // legal
    template
    A A::bar(A x) { return A(x); }
    
    // legal
    template
    A A::bar(A x) { return A(x); }
    
    // illegal!
    template
    A A::bar(A x) { return A(x); }
    

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