browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need C / C++ help?

Get answers from our community of C / C++ experts on BYTES! It's free.

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

John Ky
Guest
 
Posts: n/a
#1: Jul 22 '05
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::vector<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::create(char *name);

// Some code:
Resource resource1;
Resource resource2;

resource1.swap(Resource::open("XYZ"));
resource2.swap(Resource::create("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
Guest
 
Posts: n/a
#2: Jul 22 '05

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::vector<int>());[/color]

change to:

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

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Andrey Tarasevich
Guest
 
Posts: n/a
#3: Jul 22 '05

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(resource1);
Resource::create("abc").swap(resource2);

--
Best regards,
Andrey Tarasevich

Jeff Schwab
Guest
 
Posts: n/a
#4: Jul 22 '05

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::vector<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.

Andrew Koenig
Guest
 
Posts: n/a
#5: Jul 22 '05

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


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

Do it this way:

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


David Harmon
Guest
 
Posts: n/a
#6: Jul 22 '05

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::vector<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.

tom_usenet
Guest
 
Posts: n/a
#7: Jul 22 '05

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::vector<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
Howard Hinnant
Guest
 
Posts: n/a
#8: Jul 22 '05

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


In article <fffc30lj9mmrfv9fcjh9eefhr23hs2unco@4ax.com>,
tom_usenet <tom_usenet@hotmail.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::vector<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(stuff.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:

http://anubis.dkuug.dk/jtc1/sc22/wg2...2002/n1377.htm

I would love to see:

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

which would then allow:

x.swap(std::vector<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(InputIterator& i, Distance n);

-Howard
John Ky
Guest
 
Posts: n/a
#9: Jul 22 '05

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


Ofcourse! Thankyou everyone for your help.

Much appreciated.

-John


John Ky
Guest
 
Posts: n/a
#10: Jul 22 '05

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.cpp: In member function `ELPolicyInfo&
ELPolicyInfo::operator=(const ELPolicyInfo&)':
ELPolicyInfo.cpp:136: no matching function for call to `ELPolicyInfo::swap(
const ELPolicyInfo&)'
ELPolicyInfo.cpp:125: candidates are: void ELPolicyInfo::swap(ELPolicyInfo&)

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


Closed Thread