Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why no compilation error ?

Reply
Thread Tools

why no compilation error ?

 
 
Jarek Blakarz
Guest
Posts: n/a
 
      01-16-2013
why no compilation error

The following piece of code compiles fine.
I expected it not to compile.

I thought that fun("sth") creates temporary "string" object that cannot be
assigned to lvalue string reference.
It turns out that I was wrong.
Please help me understanding what is going on here and why it is correct.
thanks.

void fun(const string &s) {}

int main(void)
{
fun("sth");
return 0;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-16-2013
On 1/16/2013 9:38 AM, Jarek Blakarz wrote:
> why no compilation error
>
> The following piece of code compiles fine.
> I expected it not to compile.
>
> I thought that fun("sth") creates temporary "string" object that cannot be
> assigned to lvalue string reference.


A reference to a const object *can* be bound to a temporary object.
It's expressly permitted. See sections 12.2 ([class.temporary]) and
8.5.3 ([dcl.init.ref]).

> It turns out that I was wrong.
> Please help me understanding what is going on here and why it is correct.
> thanks.
>
> void fun(const string &s) {}
>
> int main(void)
> {
> fun("sth");
> return 0;
> }


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-16-2013
On 1/16/2013 6:38 AM, Jarek Blakarz wrote:
>
> I thought that fun("sth") creates temporary "string" object that cannot be
> assigned to lvalue string reference.
> It turns out that I was wrong.
> Please help me understanding what is going on here and why it is correct.


You can reproduce the same behavior with

const std::string &cr = "sth";
std::string &r = "sth";

The first will compile, while the second won't.

As you correctly noted, it implicitly creates a temporary object of type
'std::string'. In C++ it has always been possible to bind 'const'
references to temporary objects, which is why the first initialization
is valid.

--
Best regards,
Andrey Tarasevioch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compilation error with seperate compilation C__chp C++ 4 02-15-2008 03:57 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why no compilation error. sunny C Programming 14 09-20-2006 08:15 PM
Why compilation error here? Goran C++ 2 09-12-2006 11:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57