Dmitry
Dmitry

Reputation: 119

auto keyword together with smartpointers?

I can :

auto o1 = new Content;

But can't:

std::shared_ptr<auto> o1(new Content);
std::unique_ptr<auto> o1(new Content);

What I should do?

Upvotes: 3

Views: 1101

Answers (2)

Max Langhof
Max Langhof

Reputation: 23701

You can use make_shared/make_unique like this:

auto o1 = std::make_shared<Content>(); // Pass any arguments here as you would normally.
auto o2 = std::make_unique<Content>(); // Pass any arguments here as you would normally.

These functions forward all arguments to the constructor of (in this case) Content, you can use auto and you only have to write the type once.

Upvotes: 4

Quentin
Quentin

Reputation: 63134

You should:

auto o1 = std::make_unique<Content>();
auto o2 = std::make_shared<Content>();

Upvotes: 2

Related Questions