0

It seems the << operator is able to stream two string literals to cout side by side whereas it's not able to stream two string variables side by side because two string literals are implicitly concatenated whereas two string variables are not. Here's a code example to showcase this:

#include <string>
#include <iostream>
using namespace std;

int main() {
    string string1 = "C";
    string string2 = "D";

    cout << "A" "B"; //This compiles and outputs "AB"
    cout << string1 string2; //This doesn't compile

    return 0;
}

As noted in the comments the line cout << "A" "B"; compiles and outputs "AB". There seems to be some sort of implicit concatenation of these two string literals. But the next line doesn't not compile. That seems to indicate that a lack of that same implicit concatenation for some reason.

Why does the compiler treat these two lines differently? If it's concatenating string literals, shouldn't it also concatenate string variables?

What is happening implicitly and why doesn't it happen to both string literals and variables?

EDIT: It does seem close to the linked possible duplicate question but that question asks "Why is this behavior (implicit concatenation of string literals) in the language?" whereas mine asks, "Why is this done with string literals but not string variables.

13
  • 4
    This has nothing to do with <<. Commented Oct 18, 2017 at 22:53
  • 1
    Maybe this helps: How does concatenation of two string literals work? Commented Oct 18, 2017 at 22:54
  • 1
    The literals form one string (the type will be const char*). The preprocessor concats them if i'm not mistaken, so the compiler sees one string. For the std::strings they are always 2 strings (string class objects the compiler itself does nothing special with) and they need another << in between them for it to be valid syntax. Commented Oct 18, 2017 at 23:00
  • 3
    It's a feature. The compiler knows that the only thing syntactically valid to do with two string literals is to concatenate them. If it has two symbols without an operator in between it's much more indeterminate. Commented Oct 18, 2017 at 23:03
  • 1
    If you're going to downvote, please let me know why you did so I can improve the question. If it was because it was flagged as a duplicate, please explain why my explanation of why it's not a duplicate is incorrect. Commented Oct 19, 2017 at 1:16

1 Answer 1

2

Concatenation of string literals happens at compile time, in fact very early in the process of compilation, while concatenation of std::string requires generating code which calls an overloaded operator on std::string (std::basic_string<T>::operator+). Concatenation of string literals is a surface level syntactic feature which can be handled just above tokenization in C-based languages. It comes from C, not C++ and C originally didn't even have string concatenation with automatic memory allocation in its library.

(It is instructive to go through the low-level calls that happen in the following:

std::string a("a");
std::string a("b");

auto foo = a + b;
auto bar = a + "b";
auto baz = std::string("a") + b;

)

With constexpr, it might be possible to evaluate concatenation of constant strings at compile time even using std::string but one should appreciate all the machinery involved in doing so and certainly that parts of it are only fairly recent additions to C++.

In order for C++ to support the type of concatenation proposed transparently on std::string, one would have to be able to overload the "whitespace operator." See the following April Fools proposal: http://www.stroustrup.com/whitespace98.pdf .

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The detail is very much appreciated. I even learned Stroustrup has a sense a humor :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.