简体   繁体   English

仅使用一个 class 定义重载模板化 class 模板参数

[英]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.基本上,我正在尝试创建一个模板化的 class,它既可以以用户友好的方式实例化,也可以以提供更多可配置性的更复杂的方式实例化。 I want to offer these two instantiating methods WITHOUT HAVING TO DUPLICATE THE API.我想提供这两种实例化方法,而不必复制 API。

The class would look something along the lines of: class 看起来类似于:

// 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构建some_probably_awful_to_type_nested_class的通用版本,但这两个类将调用完全相同的 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.我知道我可以通过使用实际 API 的内部类型并让这两个类中的每一个类的 API 调用内部 class 来做到这一点,但我正在寻找一种无需复制代码即可执行此操作的方法。

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我很高兴使用 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.如果可能的话,我宁愿找到一个解决方案,以便用户可以简单地指定一个 int 或类型。

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> ).作为解决方法,您可以将您的值包装在一个类型 (std::integral_constant ) 中(并可能提供UDL以允许3_c而不是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:您可以使用别名包装器避免使用 inheritance:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何专门化模板类的模板化运算符重载? - How to specialize templated operator overload of template class? 包含类定义的模板容器,只需要多个模板参数之一 - Template container with contained class definition that only needs one of multiple template parameters 赋值运算符使用模板参数重载模板类 - assignment operator overload on a template class with template parameters 在具有不同模板参数集的模板化 class 中使用模板化 object - Use templated object within a templated class with different sets of template parameters 模板化类中运算符[]的可变参数化模板重载 - variadic templated overload of operator [] in templated class 如何将基本 class 成员引入派生的 class 定义,但只有一个过载? - How can I introduce base class member to derived class definition, but only one overload? operator =模板类的重载 - operator= Overload from a templated class 模板类的重载比较运算符 - Overload comparison operators for a templated class 仅为特定的模板化类启用模板 - Enable template only for specific templated class 具有多个模板参数的模板化类中的单个方法的模板专业化 - Template specialization of a single method from templated class with multiple template parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM