Accelerated C++ Solution to Exercise 1-2

Exercise 1-2

Are the following definitions valid? Why or why not?

const std::string exclam = "!";
const std::string message = "Hello " + ", world" + exclam;

Solution

No. The use of the concatenation operator is not valid. i.e. we bump into a “taboo” scenario: string literal + string literal. This is not valid.

The key in answering this question is to acknowledge the use of the string concatenation operator +.

  • It is left associative.
  • We can use + to concatenate a string and a string literal (and vice versa), or a string and a string, but not a string with a string literal (nor vice versa).

Line 1 defines a string variable exclam. This line is valid.

Line 2 defines a string variable message with the concatenation operator. The logic looks like this:

message = ( ( "Hello " + ", world" ) + exclam) 
        = ( ( a string literal + a string literal ) + a string )
        = ( ( compilation error! ) + a string)

i.e. We expect to encounter the invalid string literal + string literal scenario. This will likely cause compilation error.

Let’s test running the following program to prove the invalidity of the program.

#include <iostream>
#include <string>
int main()
{
    const std::string exclam = "!";
    const std::string message = "Hello  + ", world" + exclam;
    std::cout << message << std::endl;
    return 0;
}

Result

As expected, we get a compilation error.

error: invalid operands of types 'const char [6]' and 'const char [8]' to binary 'operator+'|

The const char [6] corresponds to the string literal “Hello”.

The const char [8] corresponds to the string literal “, world”.

(Note: I recall from reading another C++ book that the length of a string is usually the number of character plus one – that plus one is for reserving a space for a backslash, \ , character at the end)

We can easily fix this by avoiding the string literal + string literal scenario. For instance, we may imply merge the two string literal together upfront. (Keep it simple and stupid!)

    const std::string exclam = "!";
    const std::string message = "Hello, world" + exclam;

For completeness sake, let’s run the following corrected program – we should expect to avoid that compilation error.

#include <iostream>
#include <string>
int main()
{
    const std::string exclam = "!";
    const std::string message = "Hello, world" + exclam;
    std::cout << message << std::endl;
    return 0;
}

Running this program produces the output as expected.

Hello, world!

Process returned 0 (0x0)   execution time : 0.244 s
Press any key to continue.

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

One thought on “Accelerated C++ Solution to Exercise 1-2”

  1. We can use + to concatenate a string and a string literal (and vice versa), or a string and a string, but not a string with a string literal (nor vice versa).

    Did you mean at the end – but not a string literal with a string literal?

Comments are closed.