Thread: Restricting types with templates

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Restricting types with templates

    Is there a way to specify that a particular template should only work for certain types?

    Reason: I'm working on expanding my socket class to handle input. The code for converting from characters to float and characters to doubles is exactly the same save the type.

    Just doing some musing to try and save myself some typing

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I'm having trouble linking your question with your reason. Can you be more specific about what you want and what you have?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think you can only restrict a template type by the operators done on the template type passed in. You can kind of restrict the type to some Base class by doing something like this

    Code:
    Base* base = &T;

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm having trouble linking your question with your reason. Can you be more specific about what you want and what you have?
    Sure let me explain (mind you this is still in the inital phase so some parts need revision)

    I have a class that will be used to get input from a socket. The goal is to be able to use something such as:
    Code:
    int x;
    sock>>x;
    And have it get the input from the socket and convert it from the individual characters (I'm assuming Telnet at this point in time) and put the numeric value into x. Fairly simple task all in all.

    Now with floats and doubles they require exactly the same code save the fact that one has a larger memory space to store the values. So I would like to be able to do something such as:
    Code:
    template<typename T> SocketInOut &operator >> (T &);
    but have it only accept floats and doubles. The other types are simple overloads. But I don't want for example the template to accept a pointer to a float.

    Now I have thought about atof and what not but have chosen not to because:
    1) I'm not storing the "buffer" as a null terminated array of characters
    2) Its fun to do it yourself

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you want to restrict a function to a certain range of types then you don't want a template, you want simple overloading.
    My best code is written with the delete key.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok, kinda thought so. Was hoping to save myself a little bit of typing. Of course I went ahead and did it and it took a whole 5 seconds. Copy/paste, change variable type, and change a cast

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Why don't you just use stringstreams to convert the text data into the type T? If it's not possible do so(ie., a float*) then the code will give a compile time error.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can use template specialization for certain types, and then provide a default which perhaps does nothing except throw an error message, or something of that nature.

    For example, create a helper class/function that performs some operation and has specializations for float and double. For anything else, it tries its default behavior. Perhaps even a specialization for pointers (void*), that throws an error message about the type not making sense.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why don't you just use stringstreams to convert the text data into the type T?
    Because I'm not dealing with strings or streams. Mainly its because of this: I am keeping my own buffer for input of the socket. If there is nothing in the buffer or if the buffer empties while I'm still converting it has to call another function to add more to the buffer. But after I recieve it and prior to it being placed into the buffer I need to parse it to deal with certain information (ie commands).

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    On that topic, stringstream would work just dandy..but it would add some overhead that could easily be dodged by doing something more like what he has described above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  3. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  4. templates, generic data types
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2002, 02:29 AM