Thread: Default class template problem

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Default class template problem

    Say I have a class:
    Code:
    template
    <
    	typename T,
    	template<typename> class DebugPolicy = ???
    >
    class debug_ptr: public DebugPolicy<T>
    This class will derive from a specified policy.

    Now, I define two policies:
    Code:
    template<typename T> class DebugPtrDebugPolicy
    template<typename T> class DebugPtrReleasePolicy
    The idea, then, is to be able to invoke the class using:

    debug_ptr<int, DebugPtrDebugPolicy>

    Or such.
    This is all good and well, but only if I explicitly select a policy as default for the class template, such as:

    Code:
    template
    <
    	typename T,
    	template<typename> class DebugPolicy = DebugPtrDebugPolicy
    >
    class debug_ptr: public DebugPolicy<T>
    However, this is short of what I want to accomplish.
    If it's debug mode, it should automatically choose DebugPtrDebugPolicy, and if it's release mode, it should choose DebugPtrReleasePolicy.
    This is where I hit a snag.

    Determining if it's debug mode isn't a problem:
    Code:
    namespace Debug
    {
    #ifdef _DEBUG
    	static const bool IsDebugMode = true;
    	static const bool IsReleaseMode = false;
    #else
    	static const bool IsDebugMode = false;
    	static const bool IsReleaseMode = true;
    #endif
    }
    But choosing a class over the other depending on it, is.
    A sort of compile-time if.
    Of course, boost has one in the form of boost::mpl::if_c, but the prototype is:

    template<bool, typename, typename>

    So this code won't compile:
    Code:
    template
    <
    	typename T,
    	template<typename> class DebugPolicy = boost::mpl::if_c<Debug::IsDebugMode, DebugPtrDebugPolicy, DebugPtrReleasePolicy>::type
    >
    class debug_ptr: public DebugPolicy<T>
    (I'm not sure if typename is required here.)
    The reason being that DebugPtrDebugPolicy is template<typename>, a non-specialized class, and boost's if only accepts specialized classes as arguments.

    So is there any good alternative C++ way of a solution to this?
    Because I want the user to be able to choose a policy without explicitly specializing it with the type.

    I've tried all sorts of complexity, but what I got is circular template references -_-
    Anyone has any ideas on thus? I'd appreciate it.
    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.

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Metaprogramming can take care of the "compile-time if". Here's what I have in mind (I believe I remember seeing something like this in a tutorial before):
    Code:
    template <bool Condition, typename If_True, typename If_False> struct Static_If
    { typedef If_True Result_Type; }; // Assume condition is true
    
    template <typename If_True, typename If_False> struct Static_If <false, If_True, If_False>
    { typedef If_False Result_Type; }; // Specialize for false conditions
    Now, you should be able to use it like this:
    Code:
    template
    <
        typename T,
        typename DebugPolicy =
            typename Static_If <Debug::IsDebugMode, DebugPtrDebugPolicy <T>, DebugPtrReleasePolicy <T> >::Result_Type
    >
    class debug_ptr : public DebugPolicy
    {
        /* ... */
    };
    Last edited by rudyman; 07-11-2008 at 07:22 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, boost has such a helper.
    However, it defies one of the conditions.
    I should be able to create an instance of the class using:

    debug_ptr<int, DebugPtrDebugPolicy>
    And not
    debug_ptr< int, DebugPtrDebugPolicy<int> >

    That's why I was asking.
    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.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Maybe I'm too much of a C-Macro freak and not C++ template worthy, but why choose the hard, incomprehensible way when you already have that #define ?

    Code:
    template
    < typename T, class DebugPolicy = 
    #ifdef _DEBUG
            DebugPtrDebugPolicy <T>
    #else
            DebugPtrReleasePolicy <T>
    #endif
    >
    class debug_ptr : public DebugPolicy
    {
        /* ... */
    };
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Hmm, I would think the only way to do that would be by using clever macros.

    But why wouldn't you want to specialize the DebugPolicy class? If you don't want to specify the debug policy's template parameter yet, you could always use more compile-time if's within the class definition, I suppose. Consider this:

    Code:
    template <typename T>
    struct DebugPolicyType
    {
        typedef typename Static_If <Debug::IsDebugMode, DebugPtrDebugPolicy <T>, DebugPtrReleasePolicy <T> >::Result_Type type;
    };
    Then, if you wanted to use a (possibly) different parameter for the debug policy template later on, could say
    DebugPolicyType< SomeType >::type

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nvoigt View Post
    Maybe I'm too much of a C-Macro freak and not C++ template worthy, but why choose the hard, incomprehensible way when you already have that #define ?
    I've thought of that, but what's the fun in it? If we're C++ programmers, we should be able to do it the C++ way, without typing lots of code or annoying macros!

    Quote Originally Posted by rudyman View Post
    But why wouldn't you want to specialize the DebugPolicy class? If you don't want to specify the debug policy's template parameter yet, you could always use more compile-time if's within the class definition, I suppose. Consider this:
    Exactly, I don't want to specialize it YET.
    Your code works, it's the best so far, but it has some drawbacks.
    All the classes need a "type" typedef.
    It's not generic.
    And...
    debug_ptr<int>
    debug_ptr<int, DebugPtrDebugPolicy>
    ...aren't the same, even in debug mode!

    I wonder if it's possible to overcome these problems, especially the second, and if possible, the first, as well.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  2. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Template problem
    By LilShieste in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2002, 01:17 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM