Thread: A basic Q about calling templated Functions

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    A basic Q about calling templated Functions

    The declaration be :
    Code:
    template<class X,class Y> void foo(X,Y);
    When calling it, which of the following it a good practice ?...
    Code:
    foo("bar",1); // no 1
    foo<std::string,int>("bar",1);
    Both seems to be working for me....and I've seen some books sticking for the former

    ...but the documentation(err..I've only looked into the one from cplusplus)...says(implicitly implies!!) that the implicit one works(or should work) for when X==Y
    Since both i and j are of type int , and the compiler can automatically find out that the template parameter can only be int. This implicit method produces exactly the same result:
    // *that follows after an example..so i,j,int are out of scope here..!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Generally, it is considered better to allow the compiler to work out which function to call from the arguments provided (your first form). If you have to specify which instantiation of the function to call, you might as well write separate functions rather than using a template.

    Incidentally, in your example foo("bar", 1) is not equivalent to foo<std::string, int>("bar", 1). Different instantiations of foo() are called.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Different instantiations of foo() are called.
    I was under the impression that "bar" would be cast into a std::string ..seeing the explicit <std::string,int> ..in the 2nd case
    Why is a diff instantiation called instead of that..?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because "bar" is not a std::string, it is a const char*, so the first one doesn't call the same as the <std::string, int> version.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are under the wrong impression.

    A string literal "bar" is not of type std::string. It is a const array of 4 char. Implicit template instantiation finds a type that is an exact match to the supplied argument, without doing an implicit type conversion.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    so the first one doesn't call
    &&
    Implicit template instantiation finds a type that is an exact match to the supplied argument
    That was for the first one...
    Why is it wrong to say that the std::string constructor for a c_string would be called when I explicitly say that the <std::string,int> function is to be compiled ?
    Last edited by manasij7479; 06-10-2011 at 08:05 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by manasij7479 View Post
    Why is it wrong to say that the std::string constructor for a c_string would be called then I explicitly say that the <std::string,int> function is to be compiled ?
    It is not wrong to say so, because it is correct. And no one has said otherwise.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ..ok.. misunderstood an earlier reply...

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    foo("bar",1); instantiates to foo<const char[4], int>("bar",1);
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templated Classes with Templated Functions
    By -EquinoX- in forum C++ Programming
    Replies: 9
    Last Post: 11-22-2009, 10:44 PM
  2. templated functions and derivation
    By l2u in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2007, 05:27 PM
  3. Templated member functions
    By Highland Laddie in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2006, 05:25 PM
  4. templated return functions
    By karb0noxyde in forum C++ Programming
    Replies: 18
    Last Post: 10-11-2004, 05:35 PM
  5. virtual templated functions
    By ygfperson in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2003, 11:01 PM