473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Concatenating two literals...

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 '07 #1
3 1859
Th******@cpp.com 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
Mar 1 '07 #2
On Thu, 01 Mar 2007 15:09:49 -0800, Alan Johnson <aw***@yahoo.com>
wrote:
>Th******@cpp.com 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.
Mar 2 '07 #3
On 1 Mar, 22:41, TheCo...@cpp.com 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
Mar 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: foodic | last post by:
i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void...
1
by: Raymond | last post by:
How does one concat string literals in pgsql ? The compiler complains about the concat operator in the following code fragment: RAISE EXCEPTION ''SELECT DISTINCT "t1"."usesysid" '' || ''INTO...
5
by: Richard Chen | last post by:
Hi I want to construct a long string in C# in multiple lines therefore I need a symbol to concatenate each line to the next line till the end of the string. In VB, there's a symbol "_" can be...
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I canīt see...
6
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
8
by: arnuld | last post by:
i tried to output these 2 to the std. output: const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; const std::string exclam = "!"; const std::string...
7
by: Mary | last post by:
I have a student who has a hyphenated first name. If I concatenate the name like this: StudentName:( & ", " & ), it works as expected. If, however, I try to get the first name first by...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.