Templates question

This is a discussion on Templates question within the C++ Programming forums, part of the General Programming Boards category; Code: template<class T> string to_string(const T& t) { ostringstream ss; ss << t; return ss.str(); } I saw this code ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Templates question

    Code:
    template<class T>
    string to_string(const T& t)
    {
    	ostringstream ss;
    	ss << t;
    	return ss.str();
    }
    I saw this code in the book I'm reading, but I'm confused because you can call it with
    Code:
    to_string(12.34);
    //instead of
    to_string<float>(12.34);
    So why can the template parameters be excluded in this case?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Because the compiler can automatically deduce the type of T from the argument you pass.
    You only need to explicitly specify types with a class template or if the compiler cannot deduce the type automatically.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Note that to_string(12.34); is really to_string<double>(12.34); because 12.34 is a double. If you want to use the float version of the function with 12.34 then you do have to explicitly specify <float> (of course you could also just pass a float: to_string(12.34f)).

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Quote Originally Posted by Daved View Post
    ...If you want to use the float version of the function with 12.34 then you do have to explicitly specify <float>...
    Err, that would be bad. It would at least cause a warning of double to float conversion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    No, since the double is a constant, and the compiler should be able to statically prove that no loss of precision occurs. (Unless it does. I don't know if 12.34 is representable.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Well, even if you put it that way, there is no law that a compiler should do that.
    And besides... it's just plain wrong. I was discussed sometime before... using floats with doubles (or the other way around?) can introduce problems, or maybe would.

    And even so, passing a double variable into a float template would produce a warning... so I'd say it's better off avoiding it altogether. Do the right thing, so to say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Well, even if you put it that way, there is no law that a compiler should do that.
    And besides... it's just plain wrong. I was discussed sometime before... using floats with doubles (or the other way around?) can introduce problems, or maybe would.

    And even so, passing a double variable into a float template would produce a warning... so I'd say it's better off avoiding it altogether. Do the right thing, so to say.
    But how is it passing a float into a double template? If you pass a float, then the template is instantiated as a float template. T would be float, and all would be well.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Well yeah, but not if you explicitly specify the type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by Elysia View Post
    Well, even if you put it that way, there is no law that a compiler should do that.
    There is no law that it should emit a warning for truncating casts either. Warnings are completely up to the compiler.

    And besides... it's just plain wrong.
    No, it's not. Would you want the compiler to emit a warning every time you write
    Code:
    short s = 10;
    ?
    It's exactly the same situation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Quote Originally Posted by jw232 View Post
    Code:
    template<class T>
    string to_string(const T& t)
    {
    	ostringstream ss;
    	ss << t;
    	return ss.str();
    }
    I saw this code in the book I'm reading, but I'm confused because you can call it with
    Code:
    to_string(12.34);
    //instead of
    to_string<float>(12.34);
    So why can the template parameters be excluded in this case?
    even if you can omit the template parameters in such situations i would still say use them it will be helpful to you in the long run in a big project under some complex situation when you are working for 36 hours continuous except those natural breaks in between in short it simply makes the code more clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Templates question
    By tezcatlipooca in forum C++ Programming
    Replies: 9
    Last Post: 12-30-2006, 01:08 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Stack Question With Templates
    By Anonymous Freak in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2003, 11:18 AM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

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