Why does const auto &pnullptr work while auto *pnullptr doesn't in C++17?
Clash Royale CLAN TAG#URR8PPP
up vote
21
down vote
favorite
This definition works:
const auto &bnullptr;
while this fails:
auto *bnullptr;
I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".
In the second case, shouldn't b
be deduced to have some type like std::nullptr_t
?
c++ c++17
add a comment |Â
up vote
21
down vote
favorite
This definition works:
const auto &bnullptr;
while this fails:
auto *bnullptr;
I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".
In the second case, shouldn't b
be deduced to have some type like std::nullptr_t
?
c++ c++17
2
did you mean to useauto bnullptr;
?
â Sander De Dycker
yesterday
if you think the secondb
would have typestd::nullptr_t
, what type do you think the firstb
has?
â molbdnilo
yesterday
1
Am I the only one that thinksstd::nullptr_t b;
is more readable?
â rubenvb
yesterday
add a comment |Â
up vote
21
down vote
favorite
up vote
21
down vote
favorite
This definition works:
const auto &bnullptr;
while this fails:
auto *bnullptr;
I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".
In the second case, shouldn't b
be deduced to have some type like std::nullptr_t
?
c++ c++17
This definition works:
const auto &bnullptr;
while this fails:
auto *bnullptr;
I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".
In the second case, shouldn't b
be deduced to have some type like std::nullptr_t
?
c++ c++17
c++ c++17
edited yesterday
Peter Mortensen
12.9k1983111
12.9k1983111
asked yesterday
felix
929112
929112
2
did you mean to useauto bnullptr;
?
â Sander De Dycker
yesterday
if you think the secondb
would have typestd::nullptr_t
, what type do you think the firstb
has?
â molbdnilo
yesterday
1
Am I the only one that thinksstd::nullptr_t b;
is more readable?
â rubenvb
yesterday
add a comment |Â
2
did you mean to useauto bnullptr;
?
â Sander De Dycker
yesterday
if you think the secondb
would have typestd::nullptr_t
, what type do you think the firstb
has?
â molbdnilo
yesterday
1
Am I the only one that thinksstd::nullptr_t b;
is more readable?
â rubenvb
yesterday
2
2
did you mean to use
auto bnullptr;
?â Sander De Dycker
yesterday
did you mean to use
auto bnullptr;
?â Sander De Dycker
yesterday
if you think the second
b
would have type std::nullptr_t
, what type do you think the first b
has?â molbdnilo
yesterday
if you think the second
b
would have type std::nullptr_t
, what type do you think the first b
has?â molbdnilo
yesterday
1
1
Am I the only one that thinks
std::nullptr_t b;
is more readable?â rubenvb
yesterday
Am I the only one that thinks
std::nullptr_t b;
is more readable?â rubenvb
yesterday
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
30
down vote
accepted
It's because you declare b
to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.
If you want b
to be a std::nullptr_t
object, you should drop the asterisk:
auto bnullptr;
21
Ah,nullptr
, the not-a-pointer pointer literal
â Caleth
yesterday
2
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
4
@felix It ceases to be strange when you take into account thatnullptr
is convertible to both all pointer types and also all member pointer types.
â Angew
yesterday
add a comment |Â
up vote
12
down vote
decltype(nullptr)
is std::nullptr_t
.
so with
const auto &bnullptr; // auto is std::nullptr_t
// b is a reference to a temporary (with lifetime extension)
but nullptr
is NOT a pointer (even if it is convertible to).
so auto *bnullptr;
is invalid.
You might use instead
auto bnullptr; // auto is std::nullptr_t
add a comment |Â
up vote
9
down vote
nullptr
is of type std::nullptr_t
. As a nullptr
does not point to anything, there is no corresponding pointee type for std::nullptr_t
(you are not allowed to dereference a nullptr
), hence
auto *b nullptr;
requests a type that does not exist. If you want a
to be of type nullptr_t
simply write
auto b nullptr;
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
30
down vote
accepted
It's because you declare b
to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.
If you want b
to be a std::nullptr_t
object, you should drop the asterisk:
auto bnullptr;
21
Ah,nullptr
, the not-a-pointer pointer literal
â Caleth
yesterday
2
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
4
@felix It ceases to be strange when you take into account thatnullptr
is convertible to both all pointer types and also all member pointer types.
â Angew
yesterday
add a comment |Â
up vote
30
down vote
accepted
It's because you declare b
to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.
If you want b
to be a std::nullptr_t
object, you should drop the asterisk:
auto bnullptr;
21
Ah,nullptr
, the not-a-pointer pointer literal
â Caleth
yesterday
2
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
4
@felix It ceases to be strange when you take into account thatnullptr
is convertible to both all pointer types and also all member pointer types.
â Angew
yesterday
add a comment |Â
up vote
30
down vote
accepted
up vote
30
down vote
accepted
It's because you declare b
to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.
If you want b
to be a std::nullptr_t
object, you should drop the asterisk:
auto bnullptr;
It's because you declare b
to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.
If you want b
to be a std::nullptr_t
object, you should drop the asterisk:
auto bnullptr;
answered yesterday
Some programmer dude
282k23229384
282k23229384
21
Ah,nullptr
, the not-a-pointer pointer literal
â Caleth
yesterday
2
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
4
@felix It ceases to be strange when you take into account thatnullptr
is convertible to both all pointer types and also all member pointer types.
â Angew
yesterday
add a comment |Â
21
Ah,nullptr
, the not-a-pointer pointer literal
â Caleth
yesterday
2
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
4
@felix It ceases to be strange when you take into account thatnullptr
is convertible to both all pointer types and also all member pointer types.
â Angew
yesterday
21
21
Ah,
nullptr
, the not-a-pointer pointer literalâ Caleth
yesterday
Ah,
nullptr
, the not-a-pointer pointer literalâ Caleth
yesterday
2
2
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
â felix
yesterday
4
4
@felix It ceases to be strange when you take into account that
nullptr
is convertible to both all pointer types and also all member pointer types.â Angew
yesterday
@felix It ceases to be strange when you take into account that
nullptr
is convertible to both all pointer types and also all member pointer types.â Angew
yesterday
add a comment |Â
up vote
12
down vote
decltype(nullptr)
is std::nullptr_t
.
so with
const auto &bnullptr; // auto is std::nullptr_t
// b is a reference to a temporary (with lifetime extension)
but nullptr
is NOT a pointer (even if it is convertible to).
so auto *bnullptr;
is invalid.
You might use instead
auto bnullptr; // auto is std::nullptr_t
add a comment |Â
up vote
12
down vote
decltype(nullptr)
is std::nullptr_t
.
so with
const auto &bnullptr; // auto is std::nullptr_t
// b is a reference to a temporary (with lifetime extension)
but nullptr
is NOT a pointer (even if it is convertible to).
so auto *bnullptr;
is invalid.
You might use instead
auto bnullptr; // auto is std::nullptr_t
add a comment |Â
up vote
12
down vote
up vote
12
down vote
decltype(nullptr)
is std::nullptr_t
.
so with
const auto &bnullptr; // auto is std::nullptr_t
// b is a reference to a temporary (with lifetime extension)
but nullptr
is NOT a pointer (even if it is convertible to).
so auto *bnullptr;
is invalid.
You might use instead
auto bnullptr; // auto is std::nullptr_t
decltype(nullptr)
is std::nullptr_t
.
so with
const auto &bnullptr; // auto is std::nullptr_t
// b is a reference to a temporary (with lifetime extension)
but nullptr
is NOT a pointer (even if it is convertible to).
so auto *bnullptr;
is invalid.
You might use instead
auto bnullptr; // auto is std::nullptr_t
edited yesterday
answered yesterday
Jarod42
106k1296168
106k1296168
add a comment |Â
add a comment |Â
up vote
9
down vote
nullptr
is of type std::nullptr_t
. As a nullptr
does not point to anything, there is no corresponding pointee type for std::nullptr_t
(you are not allowed to dereference a nullptr
), hence
auto *b nullptr;
requests a type that does not exist. If you want a
to be of type nullptr_t
simply write
auto b nullptr;
add a comment |Â
up vote
9
down vote
nullptr
is of type std::nullptr_t
. As a nullptr
does not point to anything, there is no corresponding pointee type for std::nullptr_t
(you are not allowed to dereference a nullptr
), hence
auto *b nullptr;
requests a type that does not exist. If you want a
to be of type nullptr_t
simply write
auto b nullptr;
add a comment |Â
up vote
9
down vote
up vote
9
down vote
nullptr
is of type std::nullptr_t
. As a nullptr
does not point to anything, there is no corresponding pointee type for std::nullptr_t
(you are not allowed to dereference a nullptr
), hence
auto *b nullptr;
requests a type that does not exist. If you want a
to be of type nullptr_t
simply write
auto b nullptr;
nullptr
is of type std::nullptr_t
. As a nullptr
does not point to anything, there is no corresponding pointee type for std::nullptr_t
(you are not allowed to dereference a nullptr
), hence
auto *b nullptr;
requests a type that does not exist. If you want a
to be of type nullptr_t
simply write
auto b nullptr;
edited yesterday
answered yesterday
user463035818
14k22256
14k22256
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52274530%2fwhy-does-const-auto-pnullptr-work-while-auto-pnullptr-doesnt-in-c17%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
did you mean to use
auto bnullptr;
?â Sander De Dycker
yesterday
if you think the second
b
would have typestd::nullptr_t
, what type do you think the firstb
has?â molbdnilo
yesterday
1
Am I the only one that thinks
std::nullptr_t b;
is more readable?â rubenvb
yesterday