Name: Anonymous 2017-02-05 3:19
As I understand it, it is perfectly valid in Standard C++ to declare a class without a class-name (a so-called unnamed class) as long as at least one variable of that class type is declared before the semicolon, i.e.
This creates the variables foo and bar, each containing 3 public integer members, a, b, and c. Because there is no class-name, it is not possible for later code (even in the same file) to instantiate more variables of this type.
Since there is no class-name, it seems that the only way to declare member functions is within the class definition, as in:
But as I understand it, defining functions this way (rather than merely declaring them in the class definition and defining them elsewhere using the :: operator) tells the compiler to generate inline code rather than a function call.
Is there any way to give foo and bar a non-inline member function? I understand that the :: operator can't be used because there is no class-name, but the inability to declare non-inline functions for unnamed classes seems to be an unusual restriction, as it comes from the C++ syntax and not any inherent limitations in how computers work. Seems that there would be some way to overcome this, even if the syntax ended up being ugly and inconsistent. Maybe a
To allow member functions to be defined outside a nameless class?
class {
public:
int a, b, c;
} foo, bar;
This creates the variables foo and bar, each containing 3 public integer members, a, b, and c. Because there is no class-name, it is not possible for later code (even in the same file) to instantiate more variables of this type.
Since there is no class-name, it seems that the only way to declare member functions is within the class definition, as in:
class {
public:
int a, b, c;
int sum() {
return a + b + c;
}
} foo, bar;
But as I understand it, defining functions this way (rather than merely declaring them in the class definition and defining them elsewhere using the :: operator) tells the compiler to generate inline code rather than a function call.
Is there any way to give foo and bar a non-inline member function? I understand that the :: operator can't be used because there is no class-name, but the inability to declare non-inline functions for unnamed classes seems to be an unusual restriction, as it comes from the C++ syntax and not any inherent limitations in how computers work. Seems that there would be some way to overcome this, even if the syntax ended up being ugly and inconsistent. Maybe a
noinline
keyword, to allow functions to be defined within a class without them being inlined? Or maybe something like:int decltype(foo)::sum() {
return a + b + c;
}
To allow member functions to be defined outside a nameless class?