1

In this answer I point out that I can define a templatized method within a struct defined in a lambda. gcc gives a compiler error when I try to do this.

To the end of a Minimal, Complete, Verifiable Example I've writen a simpler example case here:

[]{struct{
    template <typename T> foo() { cout << "default\n"; }
    template <> foo<char> foo() { cout << "specialized\n"; } } result;
    return result; }

Live Example

This code runs fine in Visual Studio which can be tested at http://webcompiler.cloudapp.net/ but the linked gcc code:

Invalid declaration of member template in local class

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • It was fun while it lasted, I have edited the linked question because I believe that Visual Studio should not be allowing this code to compile. And it is definitely not cross platform standard conformant code. – Jonathan Mee May 09 '16 at 14:46

1 Answers1

3

From [temp.mem]:

A local class of non-closure type shall not have member templates.

I don't know what Visual Studio is doing.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Hmmm... I don't know my terminology well enough to know what a "Local Class of Non-Closure Type" is... can you help me with some definitions? – Jonathan Mee May 09 '16 at 14:16
  • @JonathanMee A closure type is the type of a lambda expression. A local class is a class declared within a function. – Barry May 09 '16 at 14:19
  • @JonathanMee The type of a lambda is a closure type. Everything else is a non-closure type. The `struct` is local and not a closure type, so it cannot have member templates. – Angew is no longer proud of SO May 09 '16 at 14:19
  • @Angew That's what I though... I'm lost here though, shouldn't that just say "local class"? It's almost like this is saying: "A class local to a non-closure function shall not have member types." Meaning that a class local to a closure type *could* have member templates? – Jonathan Mee May 09 '16 at 14:24
  • 1
    @JonathanMee The non-closure applies to the class type not the function that the class is local to. The closure type exception is to allow for generic lambdas (which would be local and have a member template `operator()`). – Barry May 09 '16 at 14:25
  • 1
    @JonathanMee Yes local closure types can have templates. If they could not you could not write `[](auto foo, auto bar) { do stuff };` in a function. – NathanOliver May 09 '16 at 14:25
  • @NathanOliver So what I'm getting here is that a closure-type is the only thing that can be declared locally *and* be templatized. Any other class cannot have template types if declared locally, *including if it is declared within a lambda*. Is that accurate? – Jonathan Mee May 09 '16 at 14:31
  • 3
    @JonathanMee Yes. Only a closure(lambda) can be defined at local scope and have a template member function. Any other class type cannot. I assume the reason for this is that the closure type is actually stamped out in the global space and it is just an instance of the unnamed class that is created at the local scope. – NathanOliver May 09 '16 at 14:34