A reference to non-const to be bound to a temporary object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Ky

    A reference to non-const to be bound to a temporary object

    Hello:

    I'm not sure how to word this question properly, so I'll start by
    listing the code I am having problems with:

    int main()
    {
    std::vector<int > x;
    x.swap(std::vec tor<int>());
    return 0;
    }

    This is also not allowed:

    std::vector<int > get()
    {
    return std::vector<int >();
    }

    int main()
    {
    std::vector<int > x;
    x.swap(get());
    return 0;
    }


    I was told on another newsgroup that the following code is
    nonstandard C++. The reason being that "Standard C++
    doesn't allow a reference to non-const to be bound to a
    temporary object."

    But being able to bind a reference to non-const to a temporary
    object is useful for taking advantage of using a swap method.

    I find it useful to be able to avoid this restriction because
    it allows me to wrap resources that are expensive to create
    and destroy into a class and then move it around while
    minimising copying and guaranteeing exception safety by
    using a swap function, all in a concise piece of code.

    For instance if the Resource class wrapped an expensive
    resource and the create static method creates the resource
    while the open static method opens the resource:

    // Resource interface:
    Resource Resource::open( char *name);
    Resource Resource::creat e(char *name);

    // Some code:
    Resource resource1;
    Resource resource2;

    resource1.swap( Resource::open( "XYZ"));
    resource2.swap( Resource::creat e("abc"));

    I'm therefore interested in the motivation for this restriction and
    if there is another way to do the same job just as efficiently.

    -John


  • Rob Williscroft

    #2
    Re: A reference to non-const to be bound to a temporary object

    John Ky wrote in news:1077254480 .925249@cousin. sw.oz.au:
    [color=blue]
    > Hello:
    >
    > I'm not sure how to word this question properly, so I'll start by
    > listing the code I am having problems with:
    >
    > int main()
    > {
    > std::vector<int > x;
    > x.swap(std::vec tor<int>());[/color]

    change to:

    std::vector<ont >().swap( x );
    [color=blue]
    > return 0;
    > }
    >[/color]

    HTH.

    Rob.
    --

    Comment

    • Andrey Tarasevich

      #3
      Re: A reference to non-const to be bound to a temporary object

      John Ky wrote:[color=blue]
      > ...
      > I'm therefore interested in the motivation for this restriction and
      > if there is another way to do the same job just as efficiently.
      > ...[/color]

      Just do it other way around

      std::vector<int > x;
      std::vector<int >().swap(x);

      or in your case

      Resource resource1;
      Resource resource2;

      Resource::open( "XYZ").swap(res ource1);
      Resource::creat e("abc").swap(r esource2);

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Jeff Schwab

        #4
        Re: A reference to non-const to be bound to a temporary object

        John Ky wrote:

        <snip> intro </>
        [color=blue]
        > int main()
        > {
        > std::vector<int > x;
        > x.swap(std::vec tor<int>());
        > return 0;
        > }[/color]

        <snip> more example code </>
        [color=blue]
        > I was told on another newsgroup that the following code is
        > nonstandard C++. The reason being that "Standard C++
        > doesn't allow a reference to non-const to be bound to a
        > temporary object."
        >
        > But being able to bind a reference to non-const to a temporary
        > object is useful for taking advantage of using a swap method.[/color]

        <snip/>
        [color=blue]
        > I'm therefore interested in the motivation for this restriction and
        > if there is another way to do the same job just as efficiently.[/color]

        In this particular case, x.clear( ) is probably the most efficient and
        concise option.

        Comment

        • Andrew Koenig

          #5
          Re: A reference to non-const to be bound to a temporary object

          "John Ky" <johnk@aurema.c ommercial> wrote in message
          news:1077254480 .925249@cousin. sw.oz.au...[color=blue]
          >
          > int main()
          > {
          > std::vector<int > x;
          > x.swap(std::vec tor<int>());
          > return 0;
          > }[/color]

          Do it this way:

          std::vector<int >().swap(x);


          Comment

          • David Harmon

            #6
            Re: A reference to non-const to be bound to a temporary object

            On Fri, 20 Feb 2004 15:57:51 GMT in comp.lang.c++, "Andrew Koenig"
            <ark@acm.org> was alleged to have written:
            [color=blue][color=green]
            >> x.swap(std::vec tor<int>());[/color][/color]

            [color=blue]
            >Do it this way:
            >
            > std::vector<int >().swap(x);[/color]

            The fact that one of those would work and the other not just points out
            the sillyness of the rule against binding non-const references to
            temporaries.

            Comment

            • tom_usenet

              #7
              Re: A reference to non-const to be bound to a temporary object

              On Fri, 20 Feb 2004 16:59:02 GMT, David Harmon <source@netcom. com>
              wrote:
              [color=blue]
              >On Fri, 20 Feb 2004 15:57:51 GMT in comp.lang.c++, "Andrew Koenig"
              ><ark@acm.org > was alleged to have written:
              >[color=green][color=darkred]
              >>> x.swap(std::vec tor<int>());[/color][/color]
              >
              >[color=green]
              >>Do it this way:
              >>
              >> std::vector<int >().swap(x);[/color]
              >
              >The fact that one of those would work and the other not just points out
              >the sillyness of the rule against binding non-const references to
              >temporaries.[/color]

              The rule isn't silly except in the exact type match case (of which
              vector::swap is an example).

              Tom

              C++ FAQ: http://www.parashift.com/c++-faq-lite/
              C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

              Comment

              • Howard Hinnant

                #8
                Re: A reference to non-const to be bound to a temporary object

                In article <fffc30lj9mmrfv 9fcjh9eefhr23hs 2unco@4ax.com>,
                tom_usenet <tom_usenet@hot mail.com> wrote:
                [color=blue]
                > On Fri, 20 Feb 2004 16:59:02 GMT, David Harmon <source@netcom. com>
                > wrote:
                >[color=green]
                > >On Fri, 20 Feb 2004 15:57:51 GMT in comp.lang.c++, "Andrew Koenig"
                > ><ark@acm.org > was alleged to have written:
                > >[color=darkred]
                > >>> x.swap(std::vec tor<int>());[/color]
                > >
                > >[color=darkred]
                > >>Do it this way:
                > >>
                > >> std::vector<int >().swap(x);[/color]
                > >
                > >The fact that one of those would work and the other not just points out
                > >the sillyness of the rule against binding non-const references to
                > >temporaries.[/color]
                >
                > The rule isn't silly except in the exact type match case (of which
                > vector::swap is an example).[/color]

                I would argue that there are cases when even an exact type match case
                should not bind a temporary to a non-const reference:

                std::deque<int> stuff...
                std::advance(st uff.begin(), stuff.size()/2);

                I sure am glad this call to advance doesn't compile.

                That being said, there are times when you *do* want to bind a temporary
                to a non-const reference (and swap is certainly one of those times).
                The best person to decide when to bind a temp to a ref is the author of
                the function in question. And it would even be useful to overload two
                functions based on whether or not they were binding to an rvalue or
                lvalue:

                void foo(const A& a); // bind to lvalues
                void foo(A&& a); // bind to rvalues (proposed)

                This is exactly what the move proposal is about:



                I would love to see:

                template <class T, class A>
                void
                vector<T, A>::swap(vector && v);

                which would then allow:

                x.swap(std::vec tor<int>());

                I also wouldn't mind seeing:

                template <class T> void swap(T&, T&);
                template <class T> void swap(T&&, T&);
                template <class T> void swap(T&, T&&);

                I.e. at most one of the arguments to swap can be a temporary.

                But I recommend keeping std::advance just the way it is (at least with
                respect to this issue) :-)

                template <class InputIterator, class Distance>
                void
                advance(InputIt erator& i, Distance n);

                -Howard

                Comment

                • John Ky

                  #9
                  Re: A reference to non-const to be bound to a temporary object RESOLVED

                  Ofcourse! Thankyou everyone for your help.

                  Much appreciated.

                  -John


                  Comment

                  • John Ky

                    #10
                    Re: A reference to non-const to be bound to a temporary object

                    > The rule isn't silly except in the exact type match case (of which[color=blue]
                    > vector::swap is an example).[/color]

                    The rule may not be silly, but error messages tend to be. Take gcc for
                    example:

                    ELPolicyInfo.cp p: In member function `ELPolicyInfo&
                    ELPolicyInfo::o perator=(const ELPolicyInfo&)' :
                    ELPolicyInfo.cp p:136: no matching function for call to `ELPolicyInfo:: swap(
                    const ELPolicyInfo&)'
                    ELPolicyInfo.cp p:125: candidates are: void ELPolicyInfo::s wap(ELPolicyInf o&)

                    Granted that this is a compiler issue rather than a language issue, but it
                    would
                    be nice if there were also a user-friendliness standard for C++ compilers
                    that
                    compiler implementations could strive for.

                    -John


                    Comment

                    Working...