2

I get a error for an unexpanded parameter pack while expanding it.

This code is for testing purposes.

Later i want to call a differrent function with the head, tail and element. I need the element to call another template function and want to maintain the other argument to later call a different template function.

Current output of clang (version: 8.0.0)

test.cpp

#include <iostream>

template <int... Tail>
class for_each_type {
    public:
    template <int element, int... Head>
    int each() {
        for_each_type<Tail..., element> _for;

        _for.each<Head...>();
        return element;
    }

    template <int element>
    int each() {
        return element;
    }
};

int main() {
    for_each_type<> _for;

    std::cout << _for.each<1, 1, 2, 2>() << std::endl;
}

I expect the code to output 6, but the actual output is a error message.

To run start the compiler i just run clang++ test.cpp

The current error message is:

C:\Users\HP\Documents\GitHub\CPP-Game-Engine\src>clang++ test.cpp
test.cpp:10:23: error: expected ';' after expression
        _for.each<Head...>();
                      ^
                      ;
test.cpp:10:9: error: expression contains unexpanded parameter pack 'Head'
        _for.each<Head...>();
        ^         ~~~~
test.cpp:10:23: error: expected expression
        _for.each<Head...>();
                      ^
3 errors generated.
Timo
  • 81
  • 1
  • 7
  • TL;DR: `_for.template each();` – HolyBlackCat Aug 14 '19 at 21:04
  • 1
    @HolyBlackCat Not sure if we want to dupe close this as it is only half the problem. Even after using `template` it wont compile because the function call is ambiguous when only one template parameter is used. – NathanOliver Aug 14 '19 at 21:07
  • also `template void each() { }` as ground case for recursion (removing the version with only one `int` parameter). – max66 Aug 14 '19 at 21:11
  • @NathanOliver I figured the dupe was good enough, since it solves the one error OP was stuck on. Feel free to reopen if you think the question deserves a more complete answer. – HolyBlackCat Aug 14 '19 at 21:25
  • @HolyBlackCat After more though I'll leave it. The ambiguity issue is a duplicate as well so we can add it here if the OP needs it. – NathanOliver Aug 14 '19 at 21:26

0 Answers0