有没有一种很好的方法可以在C ++中对可变参数模板中的函数参数实施类型限制?

时间:2015-11-10 23:16:56

标签: c++ variadic-templates variadic-functions enable-if

我有一个枚举,我们称之为Type。它有这样的价值:

enum Type { STRING, TYPE_A_INT, TYPE_B_INT};

我想编写一个函数Foo,可以任意使用{int,string}类型的任意值,但强制模板参数与param类型匹配。

理想情况下,它的行为如下:

Foo<STRING, TYPE_A_INT>("str", 32); // works
Foo<STRING, TYPE_B_INT>("str", 32);  // works
Foo<STRING, TYPE_B_INT, TYPE_A_INT, STRING>("str", 32, 28, "str");  // works
Foo<STRING, TYPE_B_INT>("str", "str");  // doesn't compile

有办法做到这一点吗?

似乎我可以做类似下面的事情,但是这不会起作用,因为Args会是Type,而args会是{string,int}。

template<typename Arg, typename... Args>
std::enable_if<(std::is_same<Arg, STRING>::value)> 
Foo(String arg, Args... args) {
    // Do stuff to arg, then make recursive call.
    Foo(args);
}

template<typename Arg, typename... Args>
std::enable_if<(std::is_same<Arg, TYPE_A_INT>::value)> 
Foo(int arg, Args... args) {
    // Do stuff to arg, then make recursive call.
    Foo(args);
}

我可以将参数包装成类似

的东西
pair<Type, string>
pair<Type, int>

但要避免这种情况会很好。

1 个答案:

答案 0 :(得分:3)

一种简单的方法是创建从枚举器到所需类型的映射,并使用它来构建函数参数列表 - 您可以将其视为&#34;枚举器特征&#34;,我猜:

#include <iostream>
#include <string>

enum Type {STRING, TYPE_A_INT, TYPE_B_INT};

template<Type> struct type_from;

template<> struct type_from<STRING> { using type = std::string; };
template<> struct type_from<TYPE_A_INT> { using type = int; };
template<> struct type_from<TYPE_B_INT> { using type = int; };

template<Type E> using type_from_t = typename type_from<E>::type;

template<Type... Es> void Foo(type_from_t<Es>... args)
{
   // Do stuff with args.
   using expander = int[];
   (void)expander{0, (std::cout << args << ' ', 0)...};
   std::cout << '\n';
}

int main()
{
   Foo<STRING, TYPE_A_INT>("str", 32); // works
   Foo<STRING, TYPE_B_INT>("str", 32);  // works
   Foo<STRING, TYPE_B_INT, TYPE_A_INT, STRING>("str", 32, 28, "str");  // works
   // Foo<STRING, TYPE_B_INT>("str", "str");  // doesn't work
}

如果取消注释最后一行,您将收到一条很好的错误消息,告诉您究竟哪个参数会导致问题。

当然,这并不能确保函数参数类型完全是枚举器特征给出的类型,而是确保每个参数类型都有一个有效的隐式转换。据我所知,这是你想要的,因为你的例子将字符串文字传递给std::string