By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,405 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,405 IT Pros & Developers. It's quick & easy.

(template metaprogramming) conditional inclusion of the default constructor

P: n/a
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.

Mar 17 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 16 Mar 2007 20:52:10 -0700, n.************@gmail.com wrote:
>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
contsructing each member. In other words, std::vector<std::pair<Type1, Type2>
wouldn't be possible if std::pair didn't have a default contsructor.
-dr
Mar 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.