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

"Cast" from 0 to const std::string &

39 views
Skip to first unread message

thomas.g...@gmail.com

unread,
Aug 25, 2016, 4:41:23 AM8/25/16
to
Hello,

Why exactly does the following code compile?
I would expect that it is not possible to cast (?) 0 to some const reference.
What is the C++ language rule behind this?

Thanks a lot,
Thomas


#include <string>
void f(const std::string &) {}
int main() {
f(0);
}

Ian Collins

unread,
Aug 25, 2016, 5:37:41 AM8/25/16
to
On 08/25/16 08:41 PM, thomas.g...@gmail.com wrote:
> Hello,
>
> Why exactly does the following code compile?
> I would expect that it is not possible to cast (?) 0 to some const reference.
> What is the C++ language rule behind this?
>
> #include <string>
> void f(const std::string &) {}
> int main() {
> f(0);
> }

It will construct a std::string object with the constructor that takes a
const char* parameter (0 is being interpreted as NULL).

It may compile, but it would crash if you ran it.

--
Ian

Ben Bacarisse

unread,
Aug 25, 2016, 5:56:36 AM8/25/16
to
thomas.g...@gmail.com writes:

> Why exactly does the following code compile?
> I would expect that it is not possible to cast (?) 0 to some const reference.

I agree with the "?": I would not use the word cast here. A cast is an
operator used to perform explicit conversions, and there is no cast
operator in the code.

What's more, the part you are asking about is not really a type
conversion at all; instead, a temporary std::string object is being
created.

> What is the C++ language rule behind this?

One of the constructors for std::string has a single const char *
parameter, and the literal 0 is a valid argument for that constructor.
There are lots of language rules that are needed to explain the gory
details but this might be enough explanation for now.

<snip>
> #include <string>
> void f(const std::string &) {}
> int main() {
> f(0);
> }

Calling this std::string constructor with a null pointer as the argument
is undefined behaviour, so the code might compile but it's not valid.

--
Ben.

thomas.g...@gmail.com

unread,
Aug 25, 2016, 5:57:34 AM8/25/16
to
Am Donnerstag, 25. August 2016 11:37:41 UTC+2 schrieb Ian Collins:
Thank you, that helps!

Thomas

thomas.g...@gmail.com

unread,
Aug 25, 2016, 6:00:09 AM8/25/16
to
Am Donnerstag, 25. August 2016 11:56:36 UTC+2 schrieb Ben Bacarisse:
Thanks, that helps!

Thomas

bitrex

unread,
Aug 25, 2016, 11:38:56 AM8/25/16
to
Under the latest GCC with -std=C++11, it causes a runtime error:
std::logic_error essentially complaining that you're trying to construct
a string from a nullptr

thomas.g...@gmail.com

unread,
Aug 26, 2016, 4:38:29 AM8/26/16
to
Am Donnerstag, 25. August 2016 17:38:56 UTC+2 schrieb bitrex:
> On 08/25/2016 05:37 AM, Ian Collins wrote:
> > On 08/25/16 08:41 PM, thomas wrote:
> >> Hello,
> >>
> >> Why exactly does the following code compile?
> >> I would expect that it is not possible to cast (?) 0 to some const
> >> reference.
> >> What is the C++ language rule behind this?
> >>
> >> #include <string>
> >> void f(const std::string &) {}
> >> int main() {
> >> f(0);
> >> }
> >
> > It will construct a std::string object with the constructor that takes a
> > const char* parameter (0 is being interpreted as NULL).
> >
> > It may compile, but it would crash if you ran it.
> >
>
> Under the latest GCC with -std=C++11, it causes a runtime error:
> std::logic_error essentially complaining that you're trying to construct
> a string from a nullptr

Good to know!

Thanks,
Thomas
0 new messages