Search code examples
c++variadic-templatesparameter-pack

Parameter pack expansion not compiling in c++


In c++, when I try to expand a parameter pack, it gives me the errors

"parameter packs not expanded with '...'" and "error: expected ';' before '...' token"

Help would be very appreciated. I use mingw 8.2.0.

Code:

#include <iostream>
using namespace std;

template <class... Types>
class Test {
    Test(Types... args) {
        cout << args... << endl;
    }
};

int main() {
    Test<string, int> test("this is a test", 3);
}

Solution

  • Your way makes std::cout.operator<< (oneoperand) be std::cout.operator<<( operand1, operand2, ...). You should use a thing like the following

    template <class... Types>
    struct Test {
        Test(const Types &... args) {
    
            (std::cout << ... << args) << std::endl;
        }
    };