473,785 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1879
Th******@cpp.co m 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.co m>
wrote:
>Th******@cpp.c om 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.co m 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("somet hing"), 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
1758
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 doubleMe()
1
7549
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 userID '' || ''FROM "pg_user" AS "t1" '' || ''WHERE "t1"."usename" = current_user '' ||
5
4660
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 used at the end of each line. So what is the equivalent in C#? I don't like use "+" to separate a long string into multiple short ones since it incurrs more double quotes.
4
2350
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 them in the debugger). After encoding the string (the sameone which complete value is not visible from the debugger) all the values can be seen but they are spaced by big amounts of zeros and use more that the 2048 bytes allocated. It is like if...
6
2740
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, "Item 1"); list_add(menu_items, "Item 2"); list_add(menu_items, "Item 3"); menu_create(menu_items, false)
21
2331
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 solve in this small code, I can solve it on my own program...you don't want to have head-ache :-) So, the problem excatly is, I built an array..just like this one.
8
2963
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 message2 = "hello" + " world" + exclam; the first one runs fine but 2nd does not as we can not combine 2
7
2785
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 concatenating like this: StudentName: ( & " " & ), only the first name appears. I sure would appreciate any help! Mary
27
2568
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 Standard is saying that "object is a region of storage", I deduce from that that literal constants aren't objects because they may not be alocated as regions of storage in the memory.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.