Search code examples
c++templatessyntaxcompiler-errorsfold-expression

When are fold expressions as template parameters allowed?


I have the following code:

#include <type_traits>

// ok
template <typename... Args, std::enable_if_t<(std::is_same_v<int, Args> && ...), int> = 0>
void foo(Args ...args);

template <typename T>
struct Type {

    // syntax error
    template <typename... Args, std::enable_if_t<(std::is_same_v<int, Args> && ...), int> = 0>
    Type(Args ...args);

};

This code compiles just fine on GCC, but MSVC reports:

(9): error C2059: syntax error: '...'

Apparently MSVC doesn't mind fold expressions in template parameters outside a class, but inside a class it's not okay. Another example which doesn't work is std::enable_if_t<x > y, int> = 0, because the > is interpreted as a closing pointy bracket.

I wasn't able to find resources on what expressions are allowed as template parameters at all. So what expressions are allowed and which aren't? Does the standard forbid any expressions other than other template instantiations and compilers allow some anyways?


Solution

  • Use /std:c++latest not /std::c++17.

    It looks like a MSVC bug.

    template parameters are a bit strange, but not that strange. There are limitations on declaring lambdas in some cases, but most of everything else is "constant expression" and types.