Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an aggregate initializer to return a struct in C++?

I have a function returning a named struct consisting of two ints, as follows:

struct myStruct {int i; int j;};

myStruct myFunction(int myArg){
    switch (myArg) {
    case 0: return {1,2};
    case 1: return {2,3};
    default: return {4,5};
    }
}

I want to be able to return the appropriately initialised struct from my switch statement. I can do this by declaring a named struct and initialising it, then returning the named struct, but it would be cleaner if I could have the compiler created anonymous structs for me as in my example above - which does not compile. Can this be made to work (legitimately)? Or what is the cleanest way to achieve my goal?

like image 857
StephenD Avatar asked Nov 12 '22 17:11

StephenD


1 Answers

This is perfectly valid C++11 code if we look at the draft C++ standard section 6.6.3 The return statement paragraph 2 says:

A return statement with a braced-init-list initializes the object or reference to be returned from the function by copy-list-initialization (8.5.4) from the specified initializer list.

and it provides the following example:

[ Example:
  std::pair<std::string,int> f(const char* p, int x) {
   return {p,x};
 }
—end example ]

we can see from a a live example that it works but it will not work pre- C++11 as we can see from this live example.

If you are using gcc or clang and you can not use C++11 for some reason you have the option of using compound literals as an extension in C++ even though it is a C99 feature and as we can see from this live example gcc warns:

warning: ISO C++ forbids compound-literals [-Wpedantic]

Another option is to add constructors to your struct:

struct myStruct
{
    int i; int j;
    myStruct() : i(0), j(0) {} ;    
    myStruct(int x, int y) : i(x), j(y) {} ;
};

myStruct myFunction(int myArg){
    switch (myArg) {
    case 0: return myStruct(1,2) ;
    case 1: return myStruct(2,3) ;
    default: return myStruct(4,5) ;
    }
}
like image 189
Shafik Yaghmour Avatar answered Nov 15 '22 13:11

Shafik Yaghmour