Classes defined in functions don't have to be inline!

http://coliru.stacked-crooked.com/a/3d504a053674f758
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

auto func()
{
    struct Test
    {
        int f();
    };
    return Test{};
}

int main()
{
    auto t = func();
    std::cout << t.f() << std::endl;
}

using func_Test = decltype(func());
int func_Test::f()
{
    return 7;
}
To me, this seems really cool. I don't think it's very useful, though.
Last edited on
That's c++14? Interesting.
It's valid C++11 too:
http://ideone.com/YjMZYA
Guess it's not supported with clang's c++11. http://coliru.stacked-crooked.com/a/dd1f57dbb43002bf

http://coliru.stacked-crooked.com/a/33a22da6fba1dc9d
And when I use gcc 4.9 on coliru I get "main.cpp:3:11: note: deduced return type only available with -std=c++1y or -std=gnu++1y" Isn't y/z c++14?
Last edited on
Weird, I have no idea what ideone is using for its C++11 option, I just know it's GCC.

C++14 == C++1y, but C++1z is the standard after it (possibly C++17)
Last edited on
Interesting, the code compiles with GCC 4.8.2 (with -std=c++11), and only gives this warning:
warning: ‘func’ function uses ‘auto’ type specifier without trailing return type [enabled by default]
 auto func()
           ^
Last edited on
Member functions of a local class shall be defined within their class definition, if they are defined at all. - IS

http://coliru.stacked-crooked.com/a/0dab594b3f2f7b7c
Last edited on
Topic archived. No new replies allowed.