template <typename T=void> ??

This is a discussion on template <typename T=void> ?? within the C++ Programming forums, part of the General Programming Boards category; I've seen this in a lot of example code: Code: template <typename T=void> but I don't quite understand why you ...

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    template <typename T=void> ??

    I've seen this in a lot of example code:
    Code:
    template <typename T=void>
    but I don't quite understand why you would default it to void?
    You can create a void* variable, but not a void variable, so what's going on?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    What about:
    Code:
    class TemplateClass {
      T *test;
    };

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The only thing I could imagine is when T is declared as a variable like that, but then why use void* if you're using a template? Aren't void* pointers more useful in C than in C++?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by cpjust View Post
    I've seen this in a lot of example code:
    Code:
    template <typename T=void>
    but I don't quite understand why you would default it to void?
    You can create a void* variable, but not a void variable, so what's going on?
    Sometimes these things are used to deliberately cause compile errors in certain situations. For instance, if you want to restrict the types of template parameters which are allowed to be used. See boost::enable_if
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Hmm brewbuck may have a point. But I'd rather do that something like this:
    Code:
    template<typename T> class SomeClass;
    
    template<>
    class SomeClass<AllowedType1> {
    };
    Rather than defaulting to void and declaring a variable that wouldn't be allowed.

    But, true, void pointers can often be avoided in C++. So the system you read it at would probably be badly designed anyway.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by EVOEx View Post
    But, true, void pointers can often be avoided in C++. So the system you read it at would probably be badly designed anyway.
    I think I saw it in Stroustrup's C++ book.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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