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

Discussion in 'C++' started by Marc, Nov 9, 2011.

  1. Marc

    Marc Guest

    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?
     
    Marc, Nov 9, 2011
    #1
    1. Advertising

  2. Marc

    SG Guest

    On Nov 9, 5:33 pm, Marc <> wrote:
    > 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
     
    SG, Nov 14, 2011
    #2
    1. Advertising

  3. Marc

    Marc Guest

    SG wrote:

    > On Nov 9, 5:33 pm, Marc <> wrote:
    >> 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.


    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?
     
    Marc, Nov 15, 2011
    #3
  4. Marc

    Miles Bader Guest

    Marc <> writes:
    > 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

    --
    Patience, n. A minor form of despair, disguised as a virtue.
     
    Miles Bader, Nov 16, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,439
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    636
    SpamProof
    Oct 21, 2003
  3. =?Utf-8?B?ZGF2aWQ=?=

    Microsoft TreeView: Microsoft.Web.UI.WebControls.dll

    =?Utf-8?B?ZGF2aWQ=?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,655
    Patrick Olurotimi Ige
    Apr 11, 2005
  4. responsible
    Replies:
    8
    Views:
    391
    Roland Pibinger
    Jul 29, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,342
Loading...

Share This Page