Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Concatenating two literals...

Reply
Thread Tools

Concatenating two literals...

 
 
TheCoder@cpp.com
Guest
Posts: n/a
 
      03-01-2007
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 ?
 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      03-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
MrNewsReader
Guest
Posts: n/a
 
      03-02-2007
On Thu, 01 Mar 2007 15:09:49 -0800, Alan Johnson <(E-Mail Removed)>
wrote:

>(E-Mail Removed) 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.

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      03-02-2007
On 1 Mar, 22:41, (E-Mail Removed) 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


 
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
Concatenating Date and Time from two differnt fields Tamer Ibrahim ASP .Net 1 01-17-2008 12:54 PM
Concatenating two databinding expressions - how?? sydney.luu@gmail.com ASP .Net 4 03-29-2006 02:02 PM
Concatenating Audio Files in with Perl on a Mac Gary Morrison Perl 5 03-28-2006 12:45 AM
adding strings, not concatenating them Stan Horwitz Perl 2 02-15-2006 08:38 PM
Concatenating A Double Ron ASP .Net 2 07-02-2003 10:16 PM



Advertisments