C++ Logo

std-discussion

Advanced search

Type definitions in lambdas in alias template declarations

From: Matthew House <mattlloydhouse_at_[hidden]>
Date: Thu, 12 Jan 2023 22:45:16 -0500
According to 9.2.4 [dcl.typedef] paragraph 2,

  The defining-type-specifier-seq of the defining-type-id shall not
  define a class or enumeration if the alias-declaration is the
  declaration of a template-declaration.

Should this be read as disallowing not only a direct class or
enumeration definition in the defining-type-specifier-seq, but also
such a definition in a block scope introduced by a lambda expression
in the defining-type-specifier-seq? Alternatively, is there any other
clause that would prohibit such a definition?

Compilers disagree. In the following program, Clang accepts both B and
D, but GCC and MSVC reject both B and D:

  template<int> struct A;
  template<class> using B = A<([]{ class C {}; }, 0)>;
  template<class> using D = A<([]{ enum E {}; }, 0)>;
  int main() {}

At least GCC appears to be using a simple boolean flag that bans any
type definition that syntactically appears between the start and the
end of the defining-type-specifier-seq.

Received on 2023-01-13 03:45:28