Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an unnamed struct be made static?

Can you make an unnamed struct a static member of a class?

struct Foo
{
    struct namedStruct
    {
        int memb1, memb2;
    };
    static namedStruct namedStructObj;
    struct
    {
        int memb1, memb2;
    } unnamedStructObj;
};

Foo::namedStruct Foo::namedStructObj;
// The unnamed type doesn't seem to have a type you can write
like image 262
Zebrafish Avatar asked Feb 10 '18 14:02

Zebrafish


2 Answers

Yes, it's possible:

struct Foo
{
    struct
    {
        int memb1, memb2;
    } static unnamedStructObj;
};

decltype(Foo::unnamedStructObj) Foo::unnamedStructObj;

Here, as you don't have any way to reference the unnamed struct, using decltype(Foo::unnamedStructObj) makes it possible to retrieve the type of Foo::unnamedStructObj, so you can write the definition.

like image 188
Mário Feroldi Avatar answered Oct 09 '22 21:10

Mário Feroldi


You can do it with the help of decltype():

struct Foo
{
    struct namedStruct
    {
        int memb1, memb2;
    };
    static namedStruct namedStructObj;
    static struct
    {
        int memb1, memb2;
    } unnamedStructObj;
};

Foo::namedStruct Foo::namedStructObj;
decltype(Foo::unnamedStructObj) Foo::unnamedStructObj;
like image 28
llllllllll Avatar answered Oct 09 '22 21:10

llllllllll