Quick q,

What function takes precedence with a template function vs a non-template function?

I set up the following code but my result had the following output
Template
Template
even though a guide I read said it should read:
Template
Non-template

// Testing template function vs non-template function to see which function takes precedence when overloaded
template<class T>
void output(T x, T y)
{
	cout << "Template" << endl;
}

void output(string x, string y)
{
	cout << "Non-Template" << endl;

void main()
{
	output(1,5); // I understand Template is output since
// the non-template function has arguments of type string
	output("1", "k"); // ?
}
}

Recommended Answers

All 3 Replies

>>output("1", "k"); // ?

should be a string :

output(string("1"), string("k")); // ?

Whats happening is that "1" and "k" is being interpreted as a const char*

>>output("1", "k"); // ?

should be a string :

output(string("1"), string("k")); // ?

Whats happening is that "1" and "k" is being interpreted as a const char*

Thanks!

I've got #include <string> and thought that "xyz" would be interpretted as a string versus the single quotation marks ' ' which indicate its a const char* :-S ?

Thanks!

I've got #include <string> and thought that "xyz" would be interpretted as a string versus the single quotation marks ' ' which indicate its a const char* :-S ?

Nope, then it would be ambiguous.

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.