Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;枚举到模板化函数中_C++_Templates_Enums - Fatal编程技术网

C++ C++;枚举到模板化函数中

C++ C++;枚举到模板化函数中,c++,templates,enums,C++,Templates,Enums,我有一个enum类,我有一堆非常类似的代码,可以将enum转换为模板函数调用: auto func1(类型,…参数) { 开关(类型) { 案例类型::typeA:返回func1(params); 案例类型::类型B:返回func1(参数); 案例类型::typeC:返回func1(params); ... } } 自动功能2(类型,…参数) { 开关(类型) { 案例类型::类型A:返回函数2(参数); 案例类型::类型B:返回func2(参数); 案例类型::类型C:返回func2(参数);

我有一个enum类,我有一堆非常类似的代码,可以将enum转换为模板函数调用:

auto func1(类型,…参数)
{
开关(类型)
{
案例类型::typeA:返回func1(params);
案例类型::类型B:返回func1(参数);
案例类型::typeC:返回func1(params);
...
}
}
自动功能2(类型,…参数)
{
开关(类型)
{
案例类型::类型A:返回函数2(参数);
案例类型::类型B:返回func2(参数);
案例类型::类型C:返回func2(参数);
...
}
}
//更多这样的函数3,函数4。。。
我可以使用
#define
宏生成此代码。我能用模板做些什么吗? 我可以为每个枚举类型创建一个模板类,每个类包含所有函数。
但是如何按名称调用该函数?

您可以将
func1/2/3
作为模板参数传递,并将调用转发给该模板参数

typename <template <typename> typename Func, typename... Params>
auto switchFunction(Type type, Params&&... params) {
    switch (type)
    {
    case Type::typeA: return Func<Type::typeA>(std::forward<Params>(params)...);
    case Type::typeB: return Func<Type::typeB>(std::forward<Params>(params)...);
    case Type::typeC: return Func<Type::typeC>(std::forward<Params>(params)...);
    ...
    }
}
typename
自动切换功能(类型、参数和…参数){
开关(类型)
{
案例类型::类型A:返回函数(标准::转发(参数)…);
案例类型::类型B:返回函数(标准::转发(参数)…);
案例类型::类型C:返回函数(标准::转发(参数)…);
...
}
}
那你就这样用吧

auto someOtherFunc(Type type, int param1, double param2)
{
    return switchFunction<func1>(type, param1, param2);
}
auto-someOtherFunc(类型、int-param1、double-param2)
{
返回开关功能(类型、参数1、参数2);
}
要使其工作,所有版本的
func1
必须返回相同的类型


此外,对
func1
的所有调用都必须有效。不仅仅是开关选择的开关。

您可以执行以下操作:

template <typename Func, typename... Params>
auto visit(Func func, Type type, Params&&... params)
{
    switch (type)
    {
    case Type::typeA:
        return func(std::integral_constant<Type, Type::typeA>{}, std::forward<Params>(params)...);
    case Type::typeB:
        return func(std::integral_constant<Type, Type::typeB>{}, std::forward<Params>(params)...);
    case Type::typeC:
        return func(std::integral_constant<Type, Type::typeC>{}, std::forward<Params>(params)...);
    //...
    }
}
模板
自动访问(Func Func、Type Type、Params&…Params)
{
开关(类型)
{
案例类型::类型A:
返回函数(std::integral_constant{},std::forward(params)…);
案例类型::类型B:
返回函数(std::integral_constant{},std::forward(params)…);
案例类型::类型C:
返回函数(std::integral_constant{},std::forward(params)…);
//...
}
}
调用类似于:

visit([](auto t, int e){ return Func1<t()>(e); }, type, 42);
visit([](自动t,int e){return Func1(e);},type,42);

显示宏解决方案,以更清楚地了解您需要什么。还要对你需要什么做更多的解释。如果显示的代码与您的问题相关,则使用多个
。OP还必须转换模板类中的模板函数。