可变参数模板递归类型传递
Variadic template recursive types passing
我为我的问题找到了几乎令人满意的解决方案 (第 2 个答案),但我不能在另一个编译单元中使用以这种方式编写的代码,因为将代码放在头文件中会导致链接器抱怨多个函数定义并且只在头文件中放置声明会导致链接器未定义引用问题。
这是我的代码:
template <typename... types>
void foo();
template<>
void foo<>() {
return;
}
template<typename type, typename... types>
void foohelper() {
foo<types...>();
}
template <typename... types>
void foo() {
foohelper<types...>();
}
int main() {
foo<int, int>();
}
这就是我想要实现的目标:
class A {
public:
template<>
void foo<>() {
return;
}
template<typename parser, typename... parsers>
void foohelper() {
foo<parsers...>();
}
template <typename... parsers>
void foo() {
foohelper<parsers...>();
}
};
int main() {
A a;
a.foo<int, int>();
}
但这会在编译过程中导致以下错误:
explicit specialization 'void A::foo(void)' is not a specialization of a function template
有什么简单的解决方法吗?
不需要递归。这个更简单:
#include <iostream>
#include <string>
#include <typeinfo>
class A {
public:
template<typename parser>
void foohelper() {
std::cout << "handled a " << typeid(parser).name() << std::endl;
// do work here
}
template <typename... parsers>
void foo() {
using expand = int[];
(void) expand { 0, (foohelper<parsers>(), 0)... };
}
};
int main() {
A a;
a.foo<int, int, double, std::string>();
}
示例输出:
handled a i
handled a i
handled a d
handled a NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
编辑:
为了响应不符合微软编译器的要求,这里有另一个不依赖于未确定大小的数组的版本:
#include <iostream>
#include <string>
#include <typeinfo>
class A {
public:
template<typename parser>
void foohelper() {
std::cout << "handled a " << typeid(parser).name() << std::endl;
// do work here
}
template <typename... parsers>
void foo() {
// c++ strictly does not allow 0-sized arrays.
// so here we add a NOP just in case parsers is an empty type list
using expand = int[1 + sizeof...(parsers)];
(void) expand {
(foohelper<void>(), 0),
(foohelper<parsers>(), 0)...
};
}
};
// implement the NOP operation. Note specialisation is outside class definition.
template<> void
A::foohelper<void>() {}
int main() {
A a;
a.foo<int, int, double, std::string>();
a.foo<>();
}
编辑 2:
带有前缀、后缀和解析器间调用的更完整示例。写了这么多代码,您可能会开始思考,"Hey! I could implement a whole domain-specific language here!",您是对的。
然而,如果比这复杂得多,您可能会赢得同事们永远的仇恨,所以我会避免走那条路。
#include <iostream>
#include <string>
#include <typeinfo>
class A {
public:
template<typename parser>
void foohelper() {
std::cout << "handled a " << typeid(parser).name();
// do work here
}
void prepare()
{
std::cout << "starting parsers: ";
}
void separator()
{
std::cout << ", ";
}
void nothing()
{
}
void done() {
std::cout << " done!" << std::endl;
}
template <typename... parsers>
void foo() {
// c++ strictly does not allow 0-sized arrays.
// so here we add a NOP just in case parsers is an empty type list
bool between = false;
using expand = int[2 + sizeof...(parsers)];
(void) expand {
(prepare(), 0),
((between ? separator() : nothing()), between = true, foohelper<parsers>(), 0)...,
(done(), 0)
};
}
};
int main() {
A a;
a.foo<int, int, double, std::string>();
a.foo<>();
}
示例输出:
starting parsers: handled a i, handled a i, handled a d, handled a NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE done!
starting parsers: done!
我为我的问题找到了几乎令人满意的解决方案
这是我的代码:
template <typename... types>
void foo();
template<>
void foo<>() {
return;
}
template<typename type, typename... types>
void foohelper() {
foo<types...>();
}
template <typename... types>
void foo() {
foohelper<types...>();
}
int main() {
foo<int, int>();
}
这就是我想要实现的目标:
class A {
public:
template<>
void foo<>() {
return;
}
template<typename parser, typename... parsers>
void foohelper() {
foo<parsers...>();
}
template <typename... parsers>
void foo() {
foohelper<parsers...>();
}
};
int main() {
A a;
a.foo<int, int>();
}
但这会在编译过程中导致以下错误:
explicit specialization 'void A::foo(void)' is not a specialization of a function template
有什么简单的解决方法吗?
不需要递归。这个更简单:
#include <iostream>
#include <string>
#include <typeinfo>
class A {
public:
template<typename parser>
void foohelper() {
std::cout << "handled a " << typeid(parser).name() << std::endl;
// do work here
}
template <typename... parsers>
void foo() {
using expand = int[];
(void) expand { 0, (foohelper<parsers>(), 0)... };
}
};
int main() {
A a;
a.foo<int, int, double, std::string>();
}
示例输出:
handled a i
handled a i
handled a d
handled a NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
编辑:
为了响应不符合微软编译器的要求,这里有另一个不依赖于未确定大小的数组的版本:
#include <iostream>
#include <string>
#include <typeinfo>
class A {
public:
template<typename parser>
void foohelper() {
std::cout << "handled a " << typeid(parser).name() << std::endl;
// do work here
}
template <typename... parsers>
void foo() {
// c++ strictly does not allow 0-sized arrays.
// so here we add a NOP just in case parsers is an empty type list
using expand = int[1 + sizeof...(parsers)];
(void) expand {
(foohelper<void>(), 0),
(foohelper<parsers>(), 0)...
};
}
};
// implement the NOP operation. Note specialisation is outside class definition.
template<> void
A::foohelper<void>() {}
int main() {
A a;
a.foo<int, int, double, std::string>();
a.foo<>();
}
编辑 2:
带有前缀、后缀和解析器间调用的更完整示例。写了这么多代码,您可能会开始思考,"Hey! I could implement a whole domain-specific language here!",您是对的。
然而,如果比这复杂得多,您可能会赢得同事们永远的仇恨,所以我会避免走那条路。
#include <iostream>
#include <string>
#include <typeinfo>
class A {
public:
template<typename parser>
void foohelper() {
std::cout << "handled a " << typeid(parser).name();
// do work here
}
void prepare()
{
std::cout << "starting parsers: ";
}
void separator()
{
std::cout << ", ";
}
void nothing()
{
}
void done() {
std::cout << " done!" << std::endl;
}
template <typename... parsers>
void foo() {
// c++ strictly does not allow 0-sized arrays.
// so here we add a NOP just in case parsers is an empty type list
bool between = false;
using expand = int[2 + sizeof...(parsers)];
(void) expand {
(prepare(), 0),
((between ? separator() : nothing()), between = true, foohelper<parsers>(), 0)...,
(done(), 0)
};
}
};
int main() {
A a;
a.foo<int, int, double, std::string>();
a.foo<>();
}
示例输出:
starting parsers: handled a i, handled a i, handled a d, handled a NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE done!
starting parsers: done!