Reputation: 27300
If you have a type that is quite long, in a shared_ptr
, what's the easiest way to return the equivalent of a null pointer? In C++03 I was doing the following, but I'm wondering whether C++11 has introduced any better alternatives?
std::shared_ptr<
std::vector<std::pair<int, std::map<std::string, std::string>>>
> getComplicatedType()
{
// Do some checks, can't create type, so return an empty shared_ptr
return std::shared_ptr<
std::vector<std::pair<int, std::map<std::string, std::string>>>
>();
}
I realise you could use typedef
to create an alias for the type, but I'm looking for a nicer solution where the compiler can deduce the type automatically.
Upvotes: 5
Views: 8742
Reputation: 119069
return {};
This value-initializes the return type, which in the case of a smart pointer initializes it to null.
Upvotes: 7