What I am trying to achieve is mainly returning an unnamed struct from a function in C++11. In C++14, I can do this by defining the function inline and having auto
as return type, like this:
auto func()
{
struct
{
int member;
} ret;
// set ret.member
return ret;
}
However, C++11 doesn't support deducing the return type of a normal (non-lambda) function, and this only works in C++14 when the definition is done inline.
I tried the following two variations of declaring the struct in the function declaration:
auto func() -> struct { int member; };
struct { int member; } func();
Is this simply impossible to do with C++11? If so, does someone know whether this was disallowed on purpose or just nobody thought of this new use of automatically deduced types (because this only works with the functions return value being assigned to an auto
variable)?
And finally, is there any other way to achieve something similar to this? I am aware of std::tuple
, but I want to name the values; and in my use case the struct type is definitely only useful as return type of this one function, so why name it?
Aucun commentaire:
Enregistrer un commentaire