Thread: Template Defaults

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Unhappy Template Defaults

    Can I set defaults in template parameters to depend on other parameters? This is what I'm trying to do:
    Code:
    template <typename RsrcType, typename KeyType = RsrcType::KeyType, typename HashType = RsrcType::Hash>
    class RsrcCache
    {
    	// ...
    };
    The KeyType and Hash should be changable to other classes, but I want the default to be RsrcType::Hash, for example. So, if I say:
    Code:
    RsrcCache<MyClass>
    Then KeyType = MyClass::KeyType.

    This is giving compiler errors right now...
    Code:
    rsrc_cache.h:64: error: ` RsrcType::Hash' is not a type
    (One for KeyType too.)

    I could always leave the last two with no defaults, but it means more typing, and the last two are fairly dependent (KeyType moreso than Hash) on RsrcType, so the default would be nice. (I have a couple of "RsrcType" classes for different types of resources that contain subclasses of KeyType and Hash.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Works for me in VC++ 2005. Perhaps:
    Code:
    template <typename RsrcType, typename KeyType = typename RsrcType::KeyType, typename HashType = typename RsrcType::Hash>
    Are you sure the type you're using for RsrcType has a Hash typedef or inner class?

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Are you sure the type you're using for RsrcType has a Hash typedef or inner class?
    Ah! The magic typename keyword! (That made it work.) Thanks. Had no idea it could be used there.
    Great fix... "compiler: it's not a type programmer: Yes. it. is. typename."

    I actually haven't tried instantiating the template yet - these are just errors from the header. I'll probably get more errors when I try to though.
    (MinGW, gcc 3.4.5)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, I think it is more common to make typedefs inside the class rather than take them as default template arguments. Are you sure you need them as template arguments?
    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.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Elysia View Post
    Actually, I think it is more common to make typedefs inside the class rather than take them as default template arguments. Are you sure you need them as template arguments?
    Hmm. I rethought this, and it's not a bad point, for KeyType. KeyType is too closely related to the RsrcType class (it's used in the constructor, so yes, it probably ought to just be a typedef in the class.

    The hash function, however, should be determined with a default, with the user having the final say if they want a different function. I think I'm going to reduce this to two arguments.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

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, 12:54 PM

Tags for this Thread