The question is related to this answer , where it says:

In an unnamed class, it is not possible, for example, to declare constructors, destructors, operators that have a return type, or parameter types that include the name of the class.

You also, for example, cannot declare members of class data that are pointers or references to class objects.

In an unnamed class it is not allowed to declare static data members of the class.

However, if you try, then everything turns out to be somewhat wrong:

http://ideone.com/FzRqOF

The variant with the argument is superfluous - I have not finished it yet.
But the return value and the static method obviously work.

#include <iostream> using namespace std; class { public: auto operator + () -> decltype(*this) { cout << "Unary plus\n"; return *this; } static void do_smth() { cout << "Static method\n"; } void test(auto obj) { cout << "Argument\n"; } } smth; int main() { +smth; decltype(smth) b; +b; decltype(b)::do_smth(); smth.test(b); return 0; } 

A static field is obtained only with -fpermissive , but it works -fpermissive :

http://melpon.org/wandbox/permlink/vuGA7yZvmS2ltLiM

 #include <iostream> using namespace std; class { public: static int value; } smth; int decltype(smth)::value = 10; int main() { cout << decltype(smth)::value << endl; return 0; } 

    1 answer 1

    In the class example, there are neither explicitly declared constructors, nor destructors.

    There are no non-static class data member declarations in the class.

    There are no static class member data declarations in the class.

    You can declare the type of the return value of the operators using the this pointer, provided that the operators are members of the class, and not friendly functions, but there will be difficulties with the declaration of the parameters of the operators.

    As for static methods, then in the quotation that you gave, nothing is said about them. However, the question is how to declare a class type as a return type for static functions when they do not have access to this ?

    As for this announcement

     void test(auto obj) { cout << "Argument\n"; } 

    it is incorrect from the point of view of the C ++ standard.

    Compilers may have their own language extensions that do not conform to the C ++ standard, or they may have their own bugs. :)

    • Yes, about static not so understood. For some reason I thought that in general, static is nothing, but it turns out that only fields. And with non-static what is wrong? They certainly can be. - Qwertiy ♦
    • @Qwertiy Maybe I missed something, but in your example there are no declarations of non-static class members having a class type specifier. How to do it? - Vlad from Moscow
    • This I have not yet invented. The problem is that it is impossible, or that it is prohibited? Why are static fields prohibited? prog.cpp:19:13: error: static data member '<anonymous class>::value' in unnamed class [-fpermissive] static int value; - Qwertiy ♦
    • one
      @Qwertiy, obviously the standard prohibited only static members, everything else is not prohibited, But it is unlikely that this will work, anyway. Why are static members banned? Because the static member must be defined outside the class, but it cannot be defined, because no name. Under certain conditions it is not necessary to define constants, but if they are odr-used, then their definition becomes necessary and we get the same problems. Therefore, apparently, they were simply taken and banned so as not to fool their heads. - ixSci
    • one
      @ixSci, if desired ( -fpermissive ) everything turns out: melpon.org/wandbox/permlink/vuGA7yZvmS2ltLiM - Qwertiy ♦