microsoft's pair(piecewise_construct_t,...) constructor

M

Marc

Hello,

I was reading this page about the C++11 features in the next VC compiler:
http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

and was surprised by the following. Since they don't have variadic
templates, they mimic them for small enough numbers of arguments in
the standard library. And then they claim:
"pair's pair(piecewise_construct_t, tuple<Args1...>, tuple<Args2...>)
constructor had "interesting" effects. This requires N^2 overloads
(if we support up to 10-tuples, that means 121 overloads, since empty
tuples count here too)."

I am surprised by this. As far as I know, the usual way to mimic
variadic tuple is:
template <class=void,class=void,/*repeat a few more times*/class=void>
struct tuple;

so tuple<int, double> is actually tuple<int, double, void, void...>

pair's constructor then looks like:
template<class A,class B>
template<class X0,class X1,...,class X10,class Y0,...,class Y10>
pair<A,B>::pair(piecewise_construct_t, tuple<X0,...,X10> x,
tuple<Y0,...,Y10> y):
first (construct_from_tuple<A>(x)),
second(construct_from_tuple<B>(y)){}

(plus possibly some std::move or other details)
Since we are only forwarding the tuple, we don't need to know how many
of the template parameters are void, so we only need a single maximal
overload. construct_from_tuple does need overloads, but only a linear
number of them.

Does someone understand their comment?

PS: I tried to comment on that blog entry, but a few days later my
comment hasn't appeared yet. Besides, a third of the comment form is
not visible with firefox. What do you expect from Microsoft?
 
S

SG

Hello,

I was reading this page about the C++11 features in the next VC compiler:http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

and was surprised by the following. Since they don't have variadic
templates, they mimic them for small enough numbers of arguments in
the standard library. And then they claim:
"pair's pair(piecewise_construct_t, tuple<Args1...>, tuple<Args2...>)
constructor had "interesting" effects.  This requires N^2 overloads
(if we support up to 10-tuples, that means 121 overloads, since empty
tuples count here too)."

I am surprised by this. As far as I know, the usual way to mimic
variadic tuple is:
template <class=void,class=void,/*repeat a few more times*/class=void>
struct tuple;

so tuple<int, double> is actually tuple<int, double, void, void...>

pair's constructor then looks like:
template<class A,class B>
template<class X0,class X1,...,class X10,class Y0,...,class Y10>
pair<A,B>::pair(piecewise_construct_t, tuple<X0,...,X10> x,
tuple<Y0,...,Y10> y):
first (construct_from_tuple<A>(x)),
second(construct_from_tuple<B>(y)){}

But this is cheating. Here, you initialize the members 'first' and
'second' with a single argument (whatever construct_from_tuple
returns, presumable an A and a B object) whereas the standard demands
first and second to be directly constructed with the arguments of
types X0,X1,... and Y0,Y1,... respectivly.

SG
 
M

Marc

SG said:
But this is cheating. Here, you initialize the members 'first' and
'second' with a single argument (whatever construct_from_tuple
returns, presumable an A and a B object) whereas the standard demands
first and second to be directly constructed with the arguments of
types X0,X1,... and Y0,Y1,... respectivly.

construct_from_tuple<A> directly initializes an A from x, and that A
happens to be "first". The issue would be that it relies on
copy-elision, and the compiler has a flag that disables copy-elision?

If you think this is an issue, would you mind filing a bug report
against libstdc++, which uses the same construction?
 
M

Miles Bader

Marc said:
construct_from_tuple<A> directly initializes an A from x, and that A
happens to be "first". The issue would be that it relies on
copy-elision, and the compiler has a flag that disables copy-elision?

If you think this is an issue, would you mind filing a bug report
against libstdc++, which uses the same construction?

I can't comment on correctness here, but I'll note that libstdc++ can
get away with making more assumptions about the compiler's behavior
than (portable) user code, because libstdc++ is developed and
distributed along with the compiler...

-Miles
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top