简体   繁体   中英

Overload templated class template parameters with only one class definition

Basically I am trying to create a templated class that can take either be instantiated in a user friendly way or in a more complex way offering more configurability. I want to offer these two instantiating methods WITHOUT HAVING TO DUPLICATE THE API.

The class would look something along the lines of:

// templates that offer a lot of configurability
template<typename T, typename some_probably_awful_to_type_nested_class>
class has_my_api {

};

// templates that are easy to use
template<typename T, int i>
class has_my_api {

};

The idea is that I can build a generic version of some_probably_awful_to_type_nested_class with i but the two classes would call the exact same api.

I know I could do this by having an inner type with the actual API and having the API of each of these two classes just make calls to that inner class but I am looking for a way to do this without duplicating code.

Here is a more concrete example of what I'm looking for:

#define DEFAULT_CONFIG 0


//////////////////////////////////////////////////////////////////////
// Nested classes themselves
template<typename T>
class inner_nested_class {
    // some fairly complex api
};

template<typename T,
         typename inner_nested_class_t,
         int configuration_flags = DEFAULT_CONFIG>
class outer_nested_class {
    // some data structure of inner_nested_class
};

//////////////////////////////////////////////////////////////////////
// Helpers for creating nested_class from int seed
template<typename T, int i>
struct nested_class_creator;

template<typename T>
struct nested_class_creator<T, 0> {
    typedef inner_nested_class<T> type;
};

template<typename T, int i>
struct nested_class_creator {
    typedef outer_nested_class<T, typename nested_class_creator<T, i - 1>::type> type;
};


template<typename T, typename outer_nested_class_t>
class manager_class {
    manager_class() = default;
    
    int foo();

    int bar();

    int baz();
    
    // some fairly involved additional API...
};


/* What i do NOT want to do 

template<typename T, typename outer_nested_class_t>
class manager_class_api {
    manager_class<T, outer_nested_class_t> inner_m;

    int foo() { return inner_m.foo() };

    int bar() { return inner_m.bar() };

    int baz() { return inner_m.baz() };
};

template<typename T, int i>
class manager_class_api {
    manager_class<T, typename nested_class_creator<T, i>::type> inner_m;

    int foo() { return inner_m.foo() };

    int bar() { return inner_m.bar() };

    int baz() { return inner_m.baz() };
};
*/

/* 
have tried... where the unified api could be called through _manager_class

template<typename T, int i>
using _manager_class = manager_class <T, typename nested_class_creator<T, i>::type>;

template<typename T, typename outer_nested_class_t>
using _manager_class = manager_class <T, outer_nested_class_t>;

But obviously it does not work...
*/

int
main() {
    manager_class <int, outer_nested_class<int, outer_nested_class<int, inner_nested_class<int>, 0x4>, 0x3>> manager_with_user_specified_configs;

    // how do I do this?
    //manager_class <int, 3> manager_with_simply_api;
}

Is this possible to do? If so how can I do it?

I am happy using any version of C++ >= 11

Note: I am aware that I could use a preprocessor macro for this as a worse case scenario. If possible I would rather find a solution so the user could simply specify an int or type.

I don't think we can have template which accept type or value at the same type.

As workaround, you might wrap your value in a type (std::integral_constant ) (and possibly provide UDL to allow 3_c instead of std::integral_constant<int, 3> ).

template <char... cs>
auto operator ""_c ()
{
    constexpr int n = [](){
        auto res = 0;
        
        for (auto c : {cs...}) {
            res *= 10;
            res += c - '0';
        }
        return res;
    }();
    return std::integral_constant<int, n>{};
}

And so you would have:

template<typename T, typename outer_nested_class_t>
class manager_class {
    manager_class() = default;
    
    int foo();
    int bar();
    int baz();
    
    // some fairly involved additional API...
};


template<typename T, int i>
class manager_class<T, std::integral_constant<i>> :
    manager_class<T, typename nested_class_creator<T, i>::type>
{
};

You might avoid inheritance with alias wrapper:

template<typename T, typename outer_nested_class_t>
class manager_class_api {
    using type = manager_class<T, outer_nested_class_t>;
};

template<typename T, int i>
class manager_class_api<T, std::integral_constant<i>>
{
    using type = manager_class<T, typename nested_class_creator<T, i>::type>;
};

template<typename T, typename outer_nested_class_t>
using manager_class_api_t = typename manager_class_api<T, outer_nested_class_t>::type;

with usage:

manager_class_api_t<int, outer_nested_class<int, outer_nested_class<int, inner_nested_class<int>, 0x4>, 0x3>> manager_with_user_specified_configs;

manager_class_api_t<int, decltype(3_c)> manager_with_simply_api;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM