(template metaprogramming) conditional inclusion of the default constructor

N

n.torrey.pines

std::pair explicitly defines a default constructor. This makes it
impossible to instantiate it with a type that does not have a default
constructor (or it is private).

I wonder if it would be possible to define std::pair in such a way
(using template metaprogramming) that the default constructor is
defined if and only if both of pair's argument types are default-
constructible.
 
D

Dave Rahardja

std::pair explicitly defines a default constructor. This makes it
impossible to instantiate it with a type that does not have a default
constructor (or it is private).

I wonder if it would be possible to define std::pair in such a way
(using template metaprogramming) that the default constructor is
defined if and only if both of pair's argument types are default-
constructible.

You don't need template metaprogramming for that--you just need to remove the
default constructor from the class definition. Of course then you'll end up
with something /other/ than std::pair.

There are many reasons to give "value-like" classes default constructors. One
of which is that you can then have a vector or array of it without explicitly
wouldn't be possible if std::pair didn't have a default contsructor.

-dr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top