Search code examples
templatesc++11lambdavariadicvisual-studio-2015

Is it the compiler or just me: Inheriting from variadic template consisting of lambdas


I have some code which works under GCC but fails to compile under Visual Studio 2015 (which I realize is in-development but this area I think is supposed to be implemented).

template< typename... T >
class inherit : public T...
{
public:
inherit(T... t) : T(t)... {}
};

int main() {
  auto l1 = []() {};
  auto l2 = []() {};
  inherit<decltype(l1), decltype(l2)> test(l1, l2);
  return 0;
}

That's the code snippet reduced to the pure essence of it. Visual Studio says "syntax error: 'type'" on the constructor of inherit. It then spews a little trace of how it got there and concludes with "you cannot construct an instance of a lambda".

My assumption is that the expansion of T(t)... is not expanding correctly. However I may well be getting the syntax wrong.

EDIT: Sorry the question is: Am I at fault here or not? If so, what is the correct syntax?

ADDITIONAL FINDING: In keeping with the replies I've had, this does seem to be an issue with Visual Studio 2015 having a bug in this area. In testing it seems its the expansion where the constructor params are passed to the lambda base classes which is having the issue. The following test works under VS2015:

template< typename T1, typename T2, typename... T3 >
class inherit2 : public T3...
{
public:
  inherit2(T1 t1, T2 t2) : T1(t1), T2(t2) {}
};

int main() {
  auto l1 = []() {};
  auto l2 = []() {};
  inherit2<decltype(l1), decltype(l2), decltype(l1), decltype(l2)> test(l1, l2);
  return 0;
}

Solution

  • It's the compiler. A more recent MSVC, v. 19.00.23106.0 from July 2015, accepts your example as-is.

    Perhaps brace initialization syntax T{t}... would have helped. I can't find a suitable online compiler to try, though.