Mr.*_*bis 3 c++ variadic-templates c++11
这是可行的代码:
template<typename... Args> struct count;
template<>
struct count<> {
static const int value = 0;
};
template<typename T, typename... Args>
struct count<T, Args...> {
static const int value = 1 + count<Args...>::value;
};
Run Code Online (Sandbox Code Playgroud)
现在我想知道为什么我们需要部分专门化计数类模板?
我们可以这样做:
template< typename... args> struct dd; // edited according to answer but now getting error redeclared with 2 template parameters which is point below with mark %%
template<>
struct dd<>{
static const int value = 0;
};
template<typename T, typename... args> //%%
struct dd{
static const int value= 1+ dd<args...>::value;
};
Run Code Online (Sandbox Code Playgroud)
但这不起作用,但为什么呢?
任何帮助非常感谢:)
编辑:根据答案编辑解决方案.
template<>
struct dd<> {
static const int value = 0;
};
Run Code Online (Sandbox Code Playgroud)
不是专业化的
template< typename T,typename... args> struct dd;
Run Code Online (Sandbox Code Playgroud)
其中说dd总是需要至少一个参数.
旁注,已经有一种内置的方法来获取可变参数模板参数的数量,count结构可以实现为
template <class ...args>
struct count
{
static const int value = sizeof...(args);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1329 次 |
| 最近记录: |