Template syntax error with recursive event completion

Can someone tell me what is wrong with the syntax for the end of the recursion specialization below? I thought I was following all the rules.

#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");
}

I do not understand the error message:

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'
+4
source share
2 answers

This is an important bit:

Test.cpp:22:11: error: enclosing class templates are not explicitly specialized

This means that you cannot define the explicit specialization of what is a member of an unspecialized template.

class StringList<StreamType, stream>::AddStrings<> {

It is AddStringsclearly specialized, but StringListnot. This is not allowed.

+5
source

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");
}
0

All Articles