2

This is a general question about parentheses in c++.

Whilst creating a copy of a string, i wanted to initialize a new instance of a string which then gets passed as the parameter to the constructor.

Here is what i tried (example of a string copy):

const char* test_str = "hello world";
std::string copied_str(std::string(test_str));

Corresponding output of copied_str:

1

After wrapping the initialization in parentheses, it works just as intended.

Example with parentheses:

const char* test_str = "hello world";
std::string copied_str((std::string(test_str)));

Corresponding output of copied_str with parentheses:

hello world

Why is the compiler not able to execute the first example as intended without parentheses, and what exactly is the parentheses doing in the second example?

Additional question which just interests me. Why is the output of the first example 1?

Max Drießen
  • 109
  • 5
  • 2
    `std::string copied_str(std::string(test_str));` is not a `std:.string` instance, but a function declaration. Let me have a look for a dupe... – lubgr Oct 16 '20 at 09:11
  • @lubgr Try searching for "Most Vexing Parse" – Adrian Mole Oct 16 '20 at 09:12
  • 3
    Curious, what's wrong with `std::string copied_str(test_str);` ?? – WhozCraig Oct 16 '20 at 09:13
  • 1
    @WhozCraig there is nothing wrong with that, but in this example i wanted to copy an instance of `std::string` not a `const char*`. This is purely for experimental purposes. – Max Drießen Oct 16 '20 at 09:16
  • @MaxDrießen That makes a copy of your input C-style string into a `std::string`, so I ask again, what *doesn't* that give you? Did you *try* it ? – WhozCraig Oct 16 '20 at 09:17
  • 2
    The output of the first is "1" because a function can be implicitly converted to a non-null function pointer, non-null function pointers can be implicitly converted to `true`, and `true` is printed as "1" by default. – molbdnilo Oct 16 '20 at 09:18
  • 1
    clang and recent Microsoft compilers issue a warning for this. gcc sadly does not. – Jabberwocky Oct 16 '20 at 09:20
  • @WhozCraig i know that the `std::string` constructor can build an instance of a std::string using a c-style string. If i would do that i would simply pass the `const char*` to the constructor, however my question revolves around initializing an instance of a type which then gets passed to another constructor. – Max Drießen Oct 16 '20 at 09:23
  • If there's a need to do something *other* than `std::string copied_str(test_str);` I don't see it. Regardless, your confusion about what is ultimately a MVP derivative is not unusual; it often is perplexing to people that have never seen it before. – WhozCraig Oct 16 '20 at 09:25
  • @molbdnilo ah okay. After reading about the "Most Vexing Parse", that actually makes sense. – Max Drießen Oct 16 '20 at 09:26
  • @WhozCraig like i said there there is no practical need in this. This is just a question about why the compiler handles this example like it does. If i would want to create a `std::string` out of a c-style string i would obviously do it the way you mentioned earlier. – Max Drießen Oct 16 '20 at 09:29

0 Answers0