A basic Q about calling templated Functions

This is a discussion on A basic Q about calling templated Functions within the C++ Programming forums, part of the General Programming Boards category; The declaration be : Code: template<class X,class Y> void foo(X,Y); When calling it, which of the following it a good ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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..!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    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%.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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..?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,255
    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%.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ..ok.. misunderstood an earlier reply...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21