Concatenating two literals...

Discussion in 'C++' started by TheCoder@cpp.com, Mar 1, 2007.

  1. Guest

    Why does this work:

    It seems that we're concatenating two literals:

    std::cout << greeting + name + " Hello " + "There" << std::endl;


    but if you remove (greeting + name +), it doesn't work ?
    , Mar 1, 2007
    #1
    1. Advertising

  2. Alan Johnson Guest

    wrote:
    > Why does this work:
    >
    > It seems that we're concatenating two literals:
    >
    > std::cout << greeting + name + " Hello " + "There" << std::endl;
    >
    >
    > but if you remove (greeting + name +), it doesn't work ?


    operator+ is left associative. So what you get is:
    (((greeting + name)) + " Hello ") + "There"

    Assuming greeting and/or name is of type std::string, then it is easy to
    follow along as see that each subexpression is a std::string.

    Remove the (greeting + name +) and all you are left with is trying to
    add two string literals, which does not result in concatenation. If you
    want to concatenate two string literals, simply put them next to each
    other with no operator:
    std::cout << " Hello " "There" << std::endl;

    This will concatenate them AT COMPILE TIME.

    --
    Alan Johnson
    Alan Johnson, Mar 1, 2007
    #2
    1. Advertising

  3. MrNewsReader Guest

    On Thu, 01 Mar 2007 15:09:49 -0800, Alan Johnson <>
    wrote:

    > wrote:
    >> Why does this work:
    >>
    >> It seems that we're concatenating two literals:
    >>
    >> std::cout << greeting + name + " Hello " + "There" << std::endl;
    >>
    >>
    >> but if you remove (greeting + name +), it doesn't work ?

    >
    >operator+ is left associative. So what you get is:
    >(((greeting + name)) + " Hello ") + "There"


    But why are there three braces ? wouldn't two be enough ? like:

    ((greeting + name) + " Hello ") + "There"

    it seems nicer ;-)


    >
    >Assuming greeting and/or name is of type std::string, then it is easy to
    >follow along as see that each subexpression is a std::string.
    >
    >Remove the (greeting + name +) and all you are left with is trying to
    >add two string literals, which does not result in concatenation. If you
    >want to concatenate two string literals, simply put them next to each
    >other with no operator:
    >std::cout << " Hello " "There" << std::endl;
    >
    >This will concatenate them AT COMPILE TIME.
    MrNewsReader, Mar 2, 2007
    #3
  4. kwikius Guest

    On 1 Mar, 22:41, wrote:
    > Why does this work:
    >
    > It seems that we're concatenating two literals:
    >
    > std::cout << greeting + name + " Hello " + "There" << std::endl;
    >
    > but if you remove (greeting + name +), it doesn't work ?


    to concat 2 string literals just remove the '+'. Then the preprocessor
    does it for you. (Mainly useful to split a string literal over
    multiple lines).

    Welcome to another oddity of C++. If you don't like it you could
    always try the D language where the syntax you are using would work
    AFAIK


    #include <string>
    #include <iostream>

    int main()
    {
    std::string greeting("something"), name("name");
    std::cout << greeting + " " + name + " Hello " "There\n";
    }

    regards
    Andy Little
    kwikius, Mar 2, 2007
    #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. Replies:
    4
    Views:
    12,473
  2. John Goche
    Replies:
    8
    Views:
    16,430
  3. Xiao Jianfeng
    Replies:
    23
    Views:
    2,460
    Fredrik Lundh
    Nov 23, 2005
  4. arnuld
    Replies:
    8
    Views:
    546
    Old Wolf
    Feb 8, 2007
  5. Tamer Ibrahim
    Replies:
    1
    Views:
    571
    JIGNESH
    Jan 17, 2008
Loading...

Share This Page