In C++1y, it is possible for a function's return type to involve locally defined types:
auto foo(void) {
class C {};
return C();
}
The class name C
is not in scope outside the body of foo
, so you can create class instances but not specify their type:
auto x = foo(); // Type not given explicitly
decltype(foo()) y = foo(); // Provides no more information than 'auto'
Sometimes it is desirable to specify a type explicitly. That is, it is useful to write "the type C that is defined in foo" instead of "whatever type foo returns." Is there a way to write the type of foo
's return value explicitly?
return
statement, so you couldn't define a local type there either). – Torrasusing foo_C = decltype(foo());
. – Bren