Difference when omitting the argument list of a C ++ template template

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

template<class T> Vec2<T> Vec2<T>::operator+ (const Vec2 &v) const { return Vec2(x + vx, y + vy); } 

If you embed code, it actually compiles without any list of arguments. But is it really the same as in the next version?

 template<class T> Vec2<T> Vec2<T>::operator+ (const Vec2<T> &v) const { return Vec2<T>(x + vx, y + vy); } 
+4
source share
1 answer

Inside the class, you can omit the class type argument:

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

Outside the scope of the class, you need template arguments:

 // legal template<typename K> A<K> foo(A<K> x) { return A<K>(); } // illegal! template<typename K> A foo(A x) { return A(); } 

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

 // legal template<typename K> A<K> A<K>::bar(A<K> x) { return A<K>(x); } // legal template<typename K> A<K> A<K>::bar(A x) { return A(x); } // illegal! template<typename K> AA::bar(A<K> x) { return A<K>(x); } 
+4
source

All Articles