pre*_*eys 4 c++ templates c++11
有人能告诉我下面的递归特化结束语法有什么问题吗?我以为我遵守了所有的规则.
#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");
}
Run Code Online (Sandbox Code Playgroud)
我不明白错误信息:
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'
Run Code Online (Sandbox Code Playgroud)
这是重要的一点:
Test.cpp:22:11: error: enclosing class templates are not explicitly specialized
这意味着您无法定义作为非特定模板成员的事物的显式特化.
class StringList<StreamType, stream>::AddStrings<> {
Run Code Online (Sandbox Code Playgroud)
这AddStrings是明确的专业,但StringList不是.这是不允许的.