overload function for rvalue and lvalue references

Discussion in 'C++' started by Michael Lehn, Oct 2, 2012.

  1. Michael Lehn

    Michael Lehn Guest

    Assume I have some templated class

    template <typename T>
    struct Dummy {
    // ...
    };

    And I want to overload a fucntion 'foo' such that it accepts a lvalue
    or rvalue reference of it. I can do this using some 'IsDummy trait'
    as follows:

    template <typename A>
    std::enable_if<IsDummy<A>::value, void>
    foo(A &&dummy)
    {
    // ....
    }

    If I recall correctly some C++11 draft allowed that

    template <typename A>
    foo(Dummy<A> &&dummy)
    {
    // ....
    }

    would accept both, lvalue- and value- references.

    Now my questions:
    a) Is it correct that it was allowed in some draft?
    b) Why was this feature/bug removed?

    I think I read somewhere that it was due to a conflict with "concepts".
    However, I can't find it anymore ...
     
    Michael Lehn, Oct 2, 2012
    #1
    1. Advertising

  2. Michael Lehn

    Michael Lehn Guest

    On 2012-10-02 21:49:55 +0000, Michael Lehn said:

    > Assume I have some templated class
    >
    > template <typename T>
    > struct Dummy {
    > // ...
    > };
    >
    > And I want to overload a fucntion 'foo' such that it accepts a lvalue
    > or rvalue reference of it. I can do this using some 'IsDummy trait'
    > as follows:
    >
    > template <typename A>
    > std::enable_if<IsDummy<A>::value, void>
    > foo(A &&dummy)
    > {
    > // ....
    > }
    >
    > If I recall correctly some C++11 draft allowed that
    >
    > template <typename A>
    > foo(Dummy<A> &&dummy)
    > {
    > // ....
    > }
    >
    > would accept both, lvalue- and value- references.
    >
    > Now my questions:
    > a) Is it correct that it was allowed in some draft?
    > b) Why was this feature/bug removed?
    >
    > I think I read somewhere that it was due to a conflict with "concepts".
    > However, I can't find it anymore ...


    Ok, I am actually not totally insane:

    http://blogs.msdn.com/b/vcblog/arch...-references-c-0x-features-in-vc10-part-2.aspx
     
    Michael Lehn, Oct 3, 2012
    #2
    1. Advertising

  3. Michael Lehn

    SG Guest

    Am Dienstag, 2. Oktober 2012 23:54:55 UTC+2 schrieb Michael Lehn:
    >
    > Assume I have some templated class
    >
    > template <typename T>
    > struct Dummy {
    > // ...
    > };
    >
    > And I want to overload a fucntion 'foo' such that it accepts a lvalue
    > or rvalue reference of it. I can do this using some 'IsDummy trait'
    > as follows:
    >
    > template <typename A>
    > std::enable_if<IsDummy<A>::value, void>
    > foo(A &&dummy)
    > {
    > // ....
    > }


    Or alternativly

    template<class A>
    void foo(Dummy<A> & x)
    {
    // ....
    }

    template<class A>
    inline void foo(Dummy<A> && x)
    {
    return foo(x);
    }

    But one wonders why you would want to do that. (!)

    (Note: the template argument deduction that makes perfect forwarding
    work does not apply here.)


    > If I recall correctly some C++11 draft allowed that
    >
    > template <typename A>
    > foo(Dummy<A> &&dummy)
    > {
    > // ....
    > }
    >
    > would accept both, lvalue- and value- references.
    >
    > Now my questions:
    > a) Is it correct that it was allowed in some draft?


    Yes. Up until about three years ago lvalues were allowed to bind to
    rvalue references. Now, they won't do that anymore. Note that this
    does not contradict perfect forwarding since what makes perfect
    forwarding work is
    (1) a special template argument deduction rule
    (2) reference collapsing

    > b) Why was this feature/bug removed?


    See

    "A Safety Problem with RValue References (and what to do about it)"
    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2812.html

    and

    "Fixing a Safety Problem with Rvalue References:
    Proposed Wording (Revision 1)"
    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2844.html


    Cheers!
    SG
     
    SG, Oct 3, 2012
    #3
  4. Michael Lehn

    Michael Lehn Guest

    On 2012-10-03 08:59:25 +0000, SG said:

    > Am Dienstag, 2. Oktober 2012 23:54:55 UTC+2 schrieb Michael Lehn:
    >>
    >> Assume I have some templated class
    >>
    >> template <typename T>
    >> struct Dummy {
    >> // ...
    >> };
    >>
    >> And I want to overload a fucntion 'foo' such that it accepts a lvalue
    >> or rvalue reference of it. I can do this using some 'IsDummy trait'
    >> as follows:
    >>
    >> template <typename A>
    >> std::enable_if<IsDummy<A>::value, void>
    >> foo(A &&dummy)
    >> {
    >> // ....
    >> }

    >
    > Or alternativly
    >
    > template<class A>
    > void foo(Dummy<A> & x)
    > {
    > // ....
    > }
    >
    > template<class A>
    > inline void foo(Dummy<A> && x)
    > {
    > return foo(x);
    > }
    >
    > But one wonders why you would want to do that. (!)
    >
    > (Note: the template argument deduction that makes perfect forwarding
    > work does not apply here.)
    >
    >
    >> If I recall correctly some C++11 draft allowed that
    >>
    >> template <typename A>
    >> foo(Dummy<A> &&dummy)
    >> {
    >> // ....
    >> }
    >>
    >> would accept both, lvalue- and value- references.
    >>
    >> Now my questions:
    >> a) Is it correct that it was allowed in some draft?

    >
    > Yes. Up until about three years ago lvalues were allowed to bind to
    > rvalue references. Now, they won't do that anymore. Note that this
    > does not contradict perfect forwarding since what makes perfect
    > forwarding work is
    > (1) a special template argument deduction rule
    > (2) reference collapsing
    >
    >> b) Why was this feature/bug removed?

    >
    > See
    >
    > "A Safety Problem with RValue References (and what to do about it)"
    > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2812.html
    >
    > and
    >
    > "Fixing a Safety Problem with Rvalue References:
    > Proposed Wording (Revision 1)"
    > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2844.html
    >
    >
    > Cheers!
    > SG


    Perfect! Thanks a lot for these references. That's exactly what I was
    looking for!

    Cheers,

    Michael
     
    Michael Lehn, Oct 3, 2012
    #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. Mark Stijnman
    Replies:
    2
    Views:
    514
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Apr 22, 2005
  2. Kavya
    Replies:
    9
    Views:
    541
    Dik T. Winter
    Oct 28, 2006
  3. Lighter
    Replies:
    6
    Views:
    436
    Lighter
    Jun 21, 2007
  4. Juha Nieminen
    Replies:
    13
    Views:
    654
    Edek Pienkowski
    Aug 29, 2012
  5. K. Frank
    Replies:
    4
    Views:
    190
Loading...

Share This Page