COMPILE TIME ERROR AS STD::STR

Asked By 10 points N/A Posted on -
qa-featured

Due to a bug, I just discovered that this code works well with Visual Studio 17 and probably also with other compilers. Now I am curious to know why?

#include <iostream>

#include <string>

std :: string foo () {

returns nullptr;

}int main () {

car s = foo ();

std :: cout << s << std :: endl;}

I guess it’s because the c’tor std:: basic_string can be called with a * character, implicitly converting ptr to std :: string (with NULL as argument, then poof). Am I on the right track?

SHARE
Answered By 0 points N/A #318485

COMPILE TIME ERROR AS STD::STR

qa-featured

std :: string has the following constructor:

String (const CharT * s, const Allocator & alloc = Allocator ());

This creates the string with the initialized content with a copy of the string terminated by a null character, referenced by s. The constructor is not explicit, so the implicit conversion from nullptr to std:: string is really possible.

Related Questions