While R Sahu's answer is correct, I think it's good to illustrate the case where A
is not the same as A<T>
, particularly where there are more than 1 instantiated template argument.
For example, when writing a copy constructor for a templated class with two template arguments, because the order of the arguments matters, you need to explicitly write out the templated types for the overloads.
Here is an example with a "Key/Value" type class:
#include <iostream>
// Has overloads for same class, different template order
template <class Key, class Value>
struct KV_Pair {
Key key;
Value value;
// Correct order
KV_Pair(Key key, Value value) :
key(key),
value(value) {}
// Automatically correcting to correct order
KV_Pair(Value value, Key key) :
key(key),
value(value) {}
// Copy constructor from class with right template order
KV_Pair(KV_Pair<Value, Key>& vk_pair) :
key(vk_pair.value),
value(vk_pair.key) {}
// Copy constructor from class with "wrong" template order
KV_Pair(KV_Pair<Key, Value>& vk_pair) :
key(vk_pair.key),
value(vk_pair.value) {}
};
template <class Key, class Value>
std::ostream& operator<<(std::ostream& lhs, KV_Pair<Key, Value>& rhs) {
lhs << rhs.key << ' ' << rhs.value;
return lhs;
}
int main() {
// Original order
KV_Pair<int, double> kv_pair(1, 3.14);
std::cout << kv_pair << std::endl;
// Automatically type matches for the reversed order
KV_Pair<double, int> reversed_order_pair(kv_pair);
std::cout << reversed_order_pair << std::endl;
}
See it live on Coliru.