可变参数模板方法
Variadic template method
我会创建一个类方法,该方法采用可变参数(在模板中(,然后使用它们来获取(例如(传递的参数的名称。
我试图实现(但它不起作用(:
#include <iostream>
struct St1 {};
struct St2 {};
struct St3 {};
class Test {
public:
template<typename T>
void method()
{
std::cout << typeid(T).name() << std::endl; // for example
}
template<typename T, typename ...Targs>
void method()
{
method<T>();
method<Targs...>();
}
};
int main(void) {
Test a;
a.method<St1, St2, St3>();
a.method<St1>();
}
当我想编译时,我收到这些错误,"模棱两可":
testSpeed.cpp:29:19: error: call of overloaded ‘method()’ is ambiguous
a.method<St1>();
^
testSpeed.cpp:12:10: note: candidate: void Test::method() [with T = St1]
void method()
^~~~~~
testSpeed.cpp:18:10: note: candidate: void Test::method() [with T = St1; Targs = {}]
void method()
^~~~~~
testSpeed.cpp: In instantiation of ‘void Test::method() [with T = St1; Targs = {St2, St3}]’:
testSpeed.cpp:28:29: required from here
testSpeed.cpp:20:18: error: call of overloaded ‘method()’ is ambiguous
method<T>();
~~~~~~~~~^~
testSpeed.cpp:12:10: note: candidate: void Test::method() [with T = St1]
void method()
^~~~~~
testSpeed.cpp:18:10: note: candidate: void Test::method() [with T = St1; Targs = {}]
void method()
您可以使用SFINAE 在参数包为空时禁用可变参数版本:
template<typename T, typename ...Targs,
std::enable_if_t<sizeof...(Targs) != 0>* = nullptr>
void method()
{
method<T>();
method<Targs...>();
}
演示
在 C++17 中,这非常简单:
class Test {
public:
template<typename T, typename... Ts>
void method() {
std::cout << typeid(T).name() << std::endl;
if constexpr (sizeof...(Ts) > 0)
method<Ts...>();
}
};
在 C++11 中,您可以执行以下操作:
class Test {
template<class> struct Tag {};
template<typename T>
void method_impl(Tag<T>) {
std::cout << typeid(T).name() << std::endl;
}
template<typename T, typename... Ts>
void method_impl(Tag<T> tag, Tag<Ts>... tags) {
method_impl(tag);
method_impl(tags...);
}
public:
template<typename... Ts>
void method() {
method_impl(Tag<Ts>{}...);
}
};
只需稍加修改,即可自动处理空包装:
class Test {
template<class> struct Tag {};
void method_impl() { }
template<typename T, typename... Ts>
void method_impl(Tag<T>, Tag<Ts>... tags) {
std::cout << typeid(T).name() << std::endl;
method_impl(tags...);
}
public:
template<typename... Ts>
void method() {
method_impl(Tag<Ts>{}...);
}
};
函数参数允许选择重载,而不是模板参数。
一种解决方案是将模板参数分解为参数:
template <typename ... Ts> struct Tag{};
class Test {
public:
template<typename T>
void method(Tag<T>)
{
std::cout << typeid(T).name() << std::endl; // for example
}
template<typename T, typename ...Ts>
void method(Tag<T, Ts...>)
{
method(Tag<T>{});
method(Tag<Ts...>{});
}
};
int main() {
Test a;
a.method(Tag<St1, St2, St3>{});
a.method(Tag<St1>{});
}
演示
或
template <typename T> struct Tag{};
class Test {
public:
template<typename T>
void method(Tag<T>)
{
std::cout << typeid(T).name() << std::endl; // for example
}
template<typename T, typename ...Ts>
void method(Tag<T>, Tag<Ts>...)
{
method(Tag<T>{});
method(Tag<Ts>{}...);
}
};
int main() {
Test a;
a.method(Tag<St1>{}, Tag<St2>{}, Tag<St3>{});
a.method(Tag<St1>{});
}
演示
但是你可以摆脱递归调用(和重载(:
C++17 中的折叠表达式:
template<typename ... Ts>
void method()
{
((std::cout << typeid(Ts).name() << std::endl), ...);
}
在 C++11/C++14 中,它更冗长,也不太清楚:
template<typename ... Ts>
void method()
{
std::initializer_list<int>{0, ((std::cout << typeid(Ts).name() << std::endl), 0)...};
}
相关文章:
- 将成员函数指针作为参数传递给模板方法
- 一个模板方法,用于同时接受常量和非常量参数
- 基于枚举参数调用专用模板方法
- 可变参数模板方法
- MSVC使用constexpr-if从可变模板方法中的基本模板参数中吞下const
- 是否可以基于类模板的参数调用类方法和全局方法
- 有没有办法仅在两个模板参数相同时才覆盖匹配的模板方法?
- 将方法作为模板化参数传递 C++11
- 在模板方法 c++ 中传递结构参数
- 参数列表中的模板方法"=0"
- C++创建具有可变参数模板方法的接口
- 将具有可选模板类型参数的函数传递给类构造函数并将其分配给方法
- C++11可变参数模板方法完全影子基类方法
- 将C 方法参数转换为模板参数会因编译错误而失败
- 如何将模板方法作为模板参数传递
- 类模板方法的专用化,类型名称是类模板 - 错误:参数处的类型/值不匹配
- 如何在模板方法内从参数包中移动unique_ptr
- 模板化类和非模板化类的可变参数模板方法行为的令人费解的差异
- 从模板模板方法参数中获取类型
- 模板类模板方法参数中的模板别名不起作用