When making overloaded template function is there a difference between template<> or template<type>.
For example is there a difference between

template <class T>
void dispm(T array) {
cout << "called template<class T> void dispm(string array)";
}


template<class T>
void dispm(string array) {
cout << "called template<class T> void dispm(string array)";
}

and

template <class T>
void dispm(T array) {
cout << "called template<class T> void dispm(string array)";
}


template<>
void dispm(string array) {
cout << "called template<class T> void dispm(string array)";
}

What you are doing is called Template Specialization. Consult this for more reference.

The first version contains two overloaded functions (each being a function template). The second version is a template specialization for T = string. However, in your first version, there is no way to deduce the type T from the calling context of the second function template, so you will have to provide that template argument explicitly, which will either make it not really an overload, or make it ambiguous with the first function. Anyhow, the first version will not really work, and the second version is generally not recommended.

The rule is to always overload function templates, never specialize them. That's because ADL has weird interactions between function overloading and template specialization, and there is generally nothing you can do with specialization that you can't do with overloading (or class template specialization).

In your specific case, the following is the preferred option:

//function template for dispm for any type T.
template <class T>
void dispm(T array) {
cout << "called template<class T> void dispm(T array)";
}

//function overload for dispm that takes a string parameter.
void dispm(string array) {
cout << "called void dispm(string array)";
}

For more info, just read Herb Sutter's article on the subject.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.