Jump to content

  • Log In with Google      Sign In   
  • Create Account


expected template ambiguity


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 blubberbert   Members   -  Reputation: 236

Like
1Likes
Like

Posted 22 May 2013 - 05:42 PM

i expected the following code to raise an error because the compiler doesnt know wich func to use, the template or non-template one
#include <string>
#include <iostream>
#include <cstdlib>
 
template<typename T>
T func(T arg) { return arg; }
 
std::string func(std::string arg) { return "surprise!"; }
 
 
int main(int argc, char **argv) {
    std::cout << func(std::string("no surprise please!") );
    system("PAUSE");
    return 0;
}
however the code always calls the non-template function. 
1.Why is that?
2.If that kind of behaviour is part of the standard (calling non-template functions rather than fitting templates), whats the point of the special syntax for template specialization?
i.e.
template<>
std::string func<std::string>(std::string arg) { return "surprise!"; }

------------------------------
Join the revolution and get a free donut!

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9492

Like
5Likes
Like

Posted 22 May 2013 - 05:44 PM

You probably want to read this article.

#3 blubberbert   Members   -  Reputation: 236

Like
0Likes
Like

Posted 22 May 2013 - 05:52 PM

wow that was fast.

article explains everything... holy **** thats complicated :D

thanks for helping


------------------------------
Join the revolution and get a free donut!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS