Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why is "std::string propBulkRow = 0;" legal ?

50 views
Skip to first unread message

Lynn McGuire

unread,
Apr 9, 2018, 9:52:58 PM4/9/18
to
Why is "std::string propBulkRow = 0;" legal ?

This compiles just fine on Visual C++ 2015.

Thanks,
Lynn

Melzzzzz

unread,
Apr 9, 2018, 10:19:45 PM4/9/18
to
On 2018-04-10, Lynn McGuire <lynnmc...@gmail.com> wrote:
> Why is "std::string propBulkRow = 0;" legal ?
>
> This compiles just fine on Visual C++ 2015.

Because string takes char * pointer in constructor.
Since 0 converts to NULL pointer this is legal.
Byted me several times. Recently, gcc implementation throws
exception,earlier it just segfualted.
>
> Thanks,
> Lynn


--
press any key to continue or any other to quit...

Barry Schwarz

unread,
Apr 10, 2018, 3:11:44 AM4/10/18
to
On Mon, 9 Apr 2018 20:52:46 -0500, Lynn McGuire
<lynnmc...@gmail.com> wrote:

>Why is "std::string propBulkRow = 0;" legal ?
>
>This compiles just fine on Visual C++ 2015.

Because there is an implicit conversion from the integer 0 to the
character '0x00'.

--
Remove del for email

Öö Tiib

unread,
Apr 10, 2018, 3:43:31 AM4/10/18
to
That is not the reason. The std::basic_string<> does not have
any conversion constructions from CharT.

Öö Tiib

unread,
Apr 10, 2018, 5:20:19 AM4/10/18
to
On Tuesday, 10 April 2018 04:52:58 UTC+3, Lynn McGuire wrote:
> Why is "std::string propBulkRow = 0;" legal ?
>
> This compiles just fine on Visual C++ 2015.

That has been always legal since 0 is valid pointer constant.
C++11 has added more fun:

std::string falseText{false}; // also compiles

Paavo Helde

unread,
Apr 10, 2018, 5:25:55 AM4/10/18
to
On 10.04.2018 4:52, Lynn McGuire wrote:
> Why is "std::string propBulkRow = 0;" legal ?
>
> This compiles just fine on Visual C++ 2015.
>

With gcc, you can have it failing:

> g++ test1.cpp -Werror=zero-as-null-pointer-constant

test1.cpp:3:28: error: zero as null pointer constant
[-Werror=zero-as-null-pointer-constant]
std::string propBulkRow = 0;
^

James R. Kuyper

unread,
Apr 10, 2018, 10:07:53 AM4/10/18
to
You're thinking about the implicit conversion from a null pointer
constant (which 0 qualifies as) to a null pointer of any particular
type. std::string doesn't have a constructor from a single integer type
of any size, not even "char", but it does have a constructor from a
char* value.

However, the description for that constructor has says:"Requires: s
points to an array of at least traits::length(s) + 1 elements of charT."
(24.3.2.2p10).

Note: this description is for std::basic_string<charT, traits,
Allocator>. In the context of std::string, "traits" refers to
std::char_traits<char>.

As a null pointer, (char*)0 is prohibited from pointing at any object,
so it cannot meet that requirement.

Juha Nieminen

unread,
Apr 11, 2018, 8:39:40 AM4/11/18
to
James R. Kuyper <james...@verizon.net> wrote:
> As a null pointer, (char*)0 is prohibited from pointing at any object,
> so it cannot meet that requirement.

One has to wonder why std::string couldn't accept a null char* as a valid
parameter, and interpret it has being an empty string.

James R. Kuyper

unread,
Apr 11, 2018, 9:04:59 AM4/11/18
to
An empty string is represented by "". The whole point of a null pointer
is to indicate that no object of the pointed-at type is being provided.
It's a logical error to confuse the two, and it's meaningless to provide
no string at all, as the source from which a new std::string should be
created.

Bo Persson

unread,
Apr 11, 2018, 12:21:35 PM4/11/18
to
Originally it was considered an optimization. Assuming most std::strings
are constructed from literals, a null-pointer test would be totally
wasted on those.


Bo Persson

0 new messages