【发布时间】:2018-10-22 02:41:45
【问题描述】:
我有这样的课:
template<std::size_t T, std::size_t... Args>
class A{
public:
std::array<int,summation<Args...>::value> x;
}
其中summation 定义为:
template<std::size_t size, std::size_t... sizes>
struct summation
{
static const std::size_t value = size + summation<sizes...>::value;
};
template<std::size_t size>
struct summation<size>
{
static const std::size_t value = size;
};
问题是当 Args 为空时(即我只指定 T 模板),基本情况不起作用,我收到一条编译错误消息:
error: wrong number of template arguments (0, should be 1 or more)
如何修改summation 的递归以正确处理sizeof...(Args)==0 时的情况并在这种情况下为求和返回值0?我正在使用 C++11。谢谢
注意:我还希望它可以在多线程环境中工作,在该环境中,可以由具有不同参数的不同线程同时调用求和。要在多线程环境中工作,需要进行哪些更改?谢谢
【问题讨论】:
-
你标记了C++11,所以我猜你不能使用C++17,但是模板求和是新C++17模板折叠的典型作品
-
在 C++17 中,它将是
(Args + ...)。
标签: c++ c++11 variadic-templates