why no compilation error ?

Discussion in 'C++' started by Jarek Blakarz, Jan 16, 2013.

  1. 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;
    }
    Jarek Blakarz, Jan 16, 2013
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 16, 2013
    #2
    1. Advertising

  3. 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
    Andrey Tarasevich, Jan 16, 2013
    #3
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    838
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,669
    Smokey Grindel
    Dec 2, 2006
  3. sunny

    Why no compilation error.

    sunny, Sep 20, 2006, in forum: C Programming
    Replies:
    14
    Views:
    974
    Default User
    Sep 20, 2006
  4. Goran
    Replies:
    2
    Views:
    308
    Gavin Deane
    Sep 12, 2006
  5. C__chp
    Replies:
    4
    Views:
    485
    Puppet_Sock
    Feb 15, 2008
Loading...

Share This Page