Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8365797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:44:52+00:00 2026-06-09T12:44:52+00:00

The user customizes a library template class by defining a class specifying the desired

  • 0

The user customizes a library template class by defining a class specifying the desired options. Call it a manifest. The idea is to have optional typedefs in the manifest. If the user’s manifest contains a typedef for H, for example, I want the library code to use the indicated type as its “H”. If there is no typedef in the user’s manifest, the library will use a default.

I suspect there is an elegant way to do this by utilizing the new C++11 features, but I am coming up empty. I have a solution, based on the Wikipedia entry for SFINAE. It is ugly. It requires a new template function has_typedef_H for each new H. I am vaguely uneasy that it exploits the property that 0 can mean either the integer or a null pointer. Just seems too klugey.

Is there a better way? Preferably one that will work in VC++ 2010?

In the toy example there are five classes, H1, H2, and U0, U1, and U2. H1 and H2 are examples of “helpers” for a library class L. H1 is the default. The U’s are examples of user-defined classes. In the example, I omitted defining a library class L, just using the body of main() to select the H’s based upon typedefs in the U’s (or the lack thereof). subject of type H2.


struct H1{
    void operator() (){ std::cout << "H1" << std::endl;}
};

struct H2{
    void operator() (){ std::cout << "H2" << std::endl;}
};


struct default_H: public H1 {};

struct U2 {
    typedef H2 H;
};

struct U1 {
    typedef H1 H;
};

struct U0 {
};


template <typename T>
class has_typedef_H {

    typedef char no[false+1];
    typedef char yes[true+1];

    template 
    static yes& test(typename C::H*);

    template 
    static no& test(...);

public:
    static const bool value = sizeof(test(0))-1; 
};


template<typename U, bool >
struct type_H_B: public default_H{};

template<typename U>
struct type_H_B<U, true>: public U::H {};

template<typename U>
struct H_type: public type_H_B<U, has_typedef_H<U>::value> {};

int main() {

    H_type<U0> h0;
    H_type<U1> h1;
    H_type<U2> h2;

    // Prints H1 H1 H2
    h0();
    h1();
    h2();
    return 0; 
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:44:53+00:00Added an answer on June 9, 2026 at 12:44 pm

    You don’t really need to provide a complex trait for each one of the nested types, that can be done a bit simpler. Here is an example:

    // Helper to map any type to void, needed by SFINAE below
    template <typename T>
    struct void_type {
        typedef void type;
    };
    
    // Selects a nested typedef or a default type D (using a macro to reduce boilerplate):
    #define SELECT_NESTED_TYPE( TYPE )                                       \
    template <typename T, typename D, typename _ = void>                     \
    struct select_##TYPE{                                                    \
        typedef D type;                                                      \
    };                                                                       \
    template <typename T, typename D>                                        \
    struct select_##TYPE<T, D, typename void_type<typename T::TYPE>::type> { \
        typedef typename T::TYPE type;                                       \
    };                                                                      
    
    SELECT_NESTED_TYPE( int_t );
    SELECT_NESTED_TYPE( float_t );
    //...
    #undef SELECT_NESTED_TYPE
    
    // Use
    template <typename T>
    class TheTemplate {
    public:
       typedef typename select_int_t<T,int>::type int_t;
       typedef typename select_float_t<T,double>::type float_t;
       //....
    };
    
    // Test:
    template <typename T, typename U> struct same_type {
       static const bool value = false;
    };
    template <typename T> struct same_type<T,T> {
       static const bool value = true;
    };
    struct test1 {
    };
    struct test2 {
       typedef long long int_t;
       typedef float float_t;
    };
    int main() {
       // test1 has the default typedefs
       assert(( same_type< TheTemplate<test1>::int_t, int>::value ));
       assert(( same_type< TheTemplate<test1>::float_t, double>::value ));
       // test2 has the ones in the type
       assert(( same_type< TheTemplate<test2>::int_t, long long>::value ));
       assert(( same_type< TheTemplate<test2>::float_t, float>::value ));
    }
    

    You can opt to provide a slightly simpler solution if the macro takes the default type to use and injects it in the default case (when the nested type is not defined). Admittedly, this requires the creation of the trait for each nested type, but the trait is just a couple of lines (and not too hard to define as a macro). Alternatively, if there is just a couple of potential typedefs you might be able to do without the extra boilerplate and use SFINAE directly on the destination type.


    Completely different approach… if you can

    If you can modify the types that are used in the libraries, then you can use a much simpler (although not so cool solution) by abusing inheritance. Create a base class that only holds the typedefs for the default types to be used, and have every user class derive from the class that offers the defaults. If the user wants to provide a better helper than the default they just need to provide the typedef. If they don’t provide a typedef, lookup will find the default higher up the hierarchy:

    struct default_helpers {
       typedef Helper1 helper1_t;
       typedef Helper2 helper2_t;
    // ...
    };
    struct user_type_1 : default_helpers {
    };
    struct user_type_2 : default_helpers {
       typedef MyHelper helper1_t;           // I prefer this one...
    };
    int main() {
       assert( same_type< user_type1::helper1_t, default_helpers::helper1_t >::value );
       assert( !same_type< user_type2::helper1_t, default_helpers::helper1_t >::value );
       assert( same_type< user_type1::helper2_t, user_type2::helper2_t>::value );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a class library which is contained of WPF Windows and some user
I have a document library with a workflow that dynamicly sets user permissions to
I've been looking at a lot of iOS user interfaces that have been customized.
I have been trying to create a simple application that will let the user
Is there a way to save application configurations and settings that user have customized
In my scenario, the end user customizes his or her user interface by breaking
I have a number of application settings (in user scope) for my custom grid
I have two user controls (ascx); one contains some forms, and the other one
I'm following this walktrough: http://msdn.microsoft.com/en-us/library/879kf95c.aspx to add user login and register pages using out-of-the-box
From the documentation here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.layouttemplate.aspx i had customized the user interface of login control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.