c++ - How to create a shared_ptr concisely? -


how make statement shorter:

auto ptr = std::shared_ptr<cpoint>(new cpoint(x, y)); 

please notice cpoint appears twice. can shorter?

like:

auto ptr = xxxx<cpoint>(x, y); 

xxxx macro or anything. should apply constructor parameters.

auto ptra = xxxx<classa>(); // shared_ptr new classa() auto ptrb = xxxx<classb>(a, b, c); // shared_ptr new classb(a, b, c) 

you could, , should, use function template std::make_shared:

auto p = std::make_shared<cpoint>(x, y); auto pa = std::make_shared<classa>(); // shared_ptr new classa() auto pb = std::make_shared<classb>(a, b, c); // shared_ptr new classb(a, b, c) 

to give different name, can use simple wrapper function:

template< class t, class... args> std::shared_ptr<t> foo( args&&... args ) {   return std::make_shared<t>( std::forward<args>(args)...); } 

then

auto pb = foo<classb>(a, b, c); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -