具有递归大小写结尾的模板语法错误
Template syntax error with end of recursion case
有人可以告诉我下面递归专业化结束的语法有什么问题吗? 我以为我遵守了所有规则。
#include <iostream>
template <typename StreamType = std::ostream, StreamType& stream = std::cout>
class StringList {
template <typename...> class AddStrings;
public:
template <typename... Args> void addStrings (Args&&... args) {AddStrings<Args...>()(args...);}
};
template <typename StreamType, StreamType& stream>
template <typename First, typename... Rest>
class StringList<StreamType, stream>::AddStrings<First, Rest...> : AddStrings<Rest...> {
public:
void operator()(First&& first, Rest&&... rest) {
// do whatever
AddStrings<Rest...>::operator()(std::forward<Rest>(rest)...);
}
};
template <typename StreamType, StreamType& stream>
template <>
class StringList<StreamType, stream>::AddStrings<> {
friend class StringStreamList;
void operator()() const {} // End of recursion.
};
int main() {
StringList<> stringList;
// stringList.addStrings ("dog", "cat", "bird");
}
我不明白错误消息:
Test.cpp:22:11: error: invalid explicit specialization before '>' token
template <>
^
Test.cpp:22:11: error: enclosing class templates are not explicitly specialized
Test.cpp:23:39: error: template parameters not used in partial specialization:
class StringList<StreamType, stream>::AddStrings<> {
^
Test.cpp:23:39: error: 'StreamType'
Test.cpp:23:39: error: 'stream'
这是重要的一点:
Test.cpp:22:11: error: enclosing class templates are not explicitly specialized
这意味着您无法定义作为非专用模板成员的内容的显式专用化。
class StringList<StreamType, stream>::AddStrings<> {
在这里,AddStrings
是明确的专业化的,但StringList
不是。这是不允许的。
尽管 Jonhathan Wakely 的建议有效,但对于那些强烈喜欢将此类嵌套化的人,因为它根本不在其他任何地方使用,我认为我们可以插入一个虚拟模板参数来AddStrings
。 现在我把它保留为一个内部类,因为我非常喜欢,现在编译以下内容:
#include <iostream>
template <typename StreamType = std::ostream, StreamType& stream = std::cout>
class StringList {
template <typename, typename...> struct AddStrings; // The first type is a dummy type.
public:
template <typename... Args> void addStrings (Args&&... args) {AddStrings<void, Args...>()(args...);}
};
template <typename StreamType, StreamType& stream>
template <typename T, typename First, typename... Rest>
class StringList<StreamType, stream>::AddStrings<T, First, Rest...> : AddStrings<T, Rest...> {
public:
void operator()(First&& first, Rest&&... rest) {
// do whatever
std::cout << first << ' ';
AddStrings<T, Rest...>::operator()(std::forward<Rest>(rest)...);
}
};
template <typename StreamType, StreamType& stream>
template <typename Dummy>
class StringList<StreamType, stream>::AddStrings<Dummy> {
public:
void operator()() const {} // End of recursion.
};
int main() {
StringList<> stringList;
stringList.addStrings ("dog", "cat", "bird");
}
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- c++default-int中的语法错误
- "pip install openexr"上的语法错误:错误的编译器?
- 为什么我收到语法错误:在"*"之前缺少";"
- 错误 C2059:语法错误:从结构成员声明'constant'
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误