What does the statement
return {};
in C++11 indicate, and when to use it instead of (say)
return NULL;
or
return nullptr;
What does the statement
return {};
in C++11 indicate, and when to use it instead of (say)
return NULL;
or
return nullptr;
return {};
indicates "return an object of the function's return type initialized with an empty list-initializer". The exact behaviour depends on the returned object's type.
From cppreference.com (because the OP is tagged C++11, I excluded the rules in C++14 and C++17; refer to the link for further details):
- If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.
- Otherwise, if T is an aggregate type, aggregate initialization is performed.
- Otherwise, if T is a specialization of std::initializer_list, the T object is direct-initialized or copy-initialized, depending on context, from the braced-init-list.
Otherwise, the constructors of T are considered, in two phases:
- All constructors that take std::initializer_list as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of type std::initializer_list
- If the previous stage does not produce a match, all constructors of T participate in overload resolution against the set of arguments that consists of the elements of the braced-init-list, with the restriction that only non-narrowing conversions are allowed. If this stage produces an explicit constructor as the best match for a copy-list-initialization, compilation fails (note, in simple copy-initialization, explicit constructors are not considered at all).
Otherwise (if T is not a class type), if the braced-init-list has only one element and either T isn't a reference type or is a reference type that is compatible with the type of the element, T is direct-initialized (in direct-list-initialization) or copy-initialized (in copy-list-initialization), except that narrowing conversions are not allowed.
- Otherwise, if T is a reference type that isn't compatible with the type of the element. (this fails if the reference is a non-const lvalue reference)
- Otherwise, if the braced-init-list has no elements, T is value-initialized.
Before C++11, for a function returning a std::string
, you would have written:
std::string get_string() {
return std::string();
}
Using the brace syntax in C++11, you don't need to repeat the type:
std::string get_string() {
return {}; // an empty string is returned
}
return NULL
and return nullptr
should be used when the function returns a pointer type:
any_type* get_pointer() {
return nullptr;
}
However, NULL
is deprecated since C++11 because it is just an alias to an integer value (0), while nullptr
is a real pointer type:
int get_int() {
return NULL; // will compile, NULL is an integer
}
int get_int() {
return nullptr; // error: nullptr is not an integer
}
This is probably confusing:
int foo()
{
return {}; // honestly, just return 0 - it's clearer
}
This is probably not:
SomeObjectWithADefaultConstructor foo()
{
return {};
// equivalent to return SomeObjectWithADefaultConstructor {};
}
initializer_list
constructor, wouldn't that be used if no default constructor is available? –
Hustings auto
and at least one argument. Except... if you are using a compiler that implements N3922, in that case the fun begins, because then it matters for single-value braces whether there's a =
or not... a very, very ingenious idea to make it different in every case. –
Grater {}
instead of 0
doesn't save any keystrokes. –
Wowser return {};
in the int
case –
Competitive return {}
is NOT equivalent to return SomeObjectWithADefaultConstructor{};
–
Competitive return {}
is pretty clear, once you get used to it. It's pretty easy to read it as "See the return type? It's default-constructible, so return one of those." ...Of course, if it's not default-constructible, that's when the fun begins. –
Recrimination return {};
means that {}
is the initializer for the return value. The return value is list-initialized with an empty list.
Here is some background on the return value, based on [stmt.return] in the C++ Standard:
For a function that returns by value (i.e. the return type is not a reference and not void
), there is a temporary object called the return value. This object is created by the return
statement, and its initializers depend on what was in the return statement.
The return value survives until the end of the full-expression in the code that called the function; if it has class type, then its destructor will run unless it has lifetime extended by the caller binding a reference directly to it.
The return value can be initialized in two different ways:
return some_expression;
- the return value is copy-initialized from some_expression
return { possibly_empty_list };
- the return value is
list-initialized from the list.Assuming T
is the function's return type, then note that return T{};
is different to return {}
: in the former, a temporary T{}
is created, and then the return value is copy-initialized from that temporary.
This will fail to compile if T
has no accessible copy/move-constructor, but return {};
will succeed even if those constructors are not present. Accordingly, return T{};
may show side-effects of the copy-constructor etc., although this is a copy elision context so it may not.
Here's a brief recap of list-initialization in C++14 (N4140 [dcl.init.list]/3), where the initializer is an empty list:
T
is an aggregate, then each member is initialized from its brace-or-equal-initializer if it had one, otherwise as if by {}
(so apply these steps recursively).T
is a class type with a user-provided default constructor, that constructor is called.T
is a class type with an implicitly-defined, or = default
ed default constructor, the object is zero-initialized and then the default constructor is called.T
is a std::initializer_list
, the return value is an empty such list.T
is a non-class type -- return types cannot be arrays), the return value is zero-initialized.{}
, which may or may not be value-init. –
Avaunt It's a sort of short hand for a new instance of the methods return type.
© 2022 - 2024 — McMap. All rights reserved.