What's the difference between `auto pp` and `auto *ppp`?
up vote
16
down vote
favorite
int foo = 11;
int *p = &foo;
auto pp = p;
auto *ppp = p;
cout << pp << endl;
cout << ppp << endl;
This program will produce the same output for pp
and ppp
, but why? auto
deduces the variable should be int
, so I think the declaration of ppp
is right. But pp
and ppp
have the same value...
Output:
0x61fefc
0x61fefc
c++ c++11 pointers auto
add a comment |
up vote
16
down vote
favorite
int foo = 11;
int *p = &foo;
auto pp = p;
auto *ppp = p;
cout << pp << endl;
cout << ppp << endl;
This program will produce the same output for pp
and ppp
, but why? auto
deduces the variable should be int
, so I think the declaration of ppp
is right. But pp
and ppp
have the same value...
Output:
0x61fefc
0x61fefc
c++ c++11 pointers auto
add a comment |
up vote
16
down vote
favorite
up vote
16
down vote
favorite
int foo = 11;
int *p = &foo;
auto pp = p;
auto *ppp = p;
cout << pp << endl;
cout << ppp << endl;
This program will produce the same output for pp
and ppp
, but why? auto
deduces the variable should be int
, so I think the declaration of ppp
is right. But pp
and ppp
have the same value...
Output:
0x61fefc
0x61fefc
c++ c++11 pointers auto
int foo = 11;
int *p = &foo;
auto pp = p;
auto *ppp = p;
cout << pp << endl;
cout << ppp << endl;
This program will produce the same output for pp
and ppp
, but why? auto
deduces the variable should be int
, so I think the declaration of ppp
is right. But pp
and ppp
have the same value...
Output:
0x61fefc
0x61fefc
c++ c++11 pointers auto
c++ c++11 pointers auto
edited Nov 29 at 17:40
Boann
36.6k1287121
36.6k1287121
asked Nov 29 at 10:50
廖茂生
1129
1129
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
27
down vote
In the particular example you show, there is no difference. But imagine you would later on add two const
qualifier like the following:
const auto pp = p;
const auto *ppp = p;
Is it still the same? Turns out that this is identical to
int * const pp = p; // pointer is readonly
const int *ppp = p; // pointer is readonly
because in auto pp = p
, auto
matches int*
as a whole, and const
modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p
, auto
matches int
, and this is what const
applies to.
Because of this notable difference and because we should use const
variables whenever possible, I'd advise you to always use auto*
when using type deduction for pointer variables. There is no way to const
-qualify the pointer itself instead of the pointee, and if you want to const
-qualify both, this is possible by
const auto * const pppp = p;
which doesn't work without the *
.
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup'sTour
2nd ed. (I may have missed it.)
– davidbak
Nov 29 at 15:10
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
2
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
2
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
add a comment |
up vote
7
down vote
There is no difference in auto
and auto *
in this particular case. In case of auto pp = p;
type will be deduced to int *
while in case of auto *ppp = p;
type will be deduced to int
.
auto
qualifier:
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]
Note that unlike auto
the auto *
will deduce only pointer types.
6
The key point isauto
uses template argument deduction rules, and soauto*
will only deduce pointer types.
– rustyx
Nov 29 at 10:58
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
27
down vote
In the particular example you show, there is no difference. But imagine you would later on add two const
qualifier like the following:
const auto pp = p;
const auto *ppp = p;
Is it still the same? Turns out that this is identical to
int * const pp = p; // pointer is readonly
const int *ppp = p; // pointer is readonly
because in auto pp = p
, auto
matches int*
as a whole, and const
modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p
, auto
matches int
, and this is what const
applies to.
Because of this notable difference and because we should use const
variables whenever possible, I'd advise you to always use auto*
when using type deduction for pointer variables. There is no way to const
-qualify the pointer itself instead of the pointee, and if you want to const
-qualify both, this is possible by
const auto * const pppp = p;
which doesn't work without the *
.
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup'sTour
2nd ed. (I may have missed it.)
– davidbak
Nov 29 at 15:10
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
2
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
2
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
add a comment |
up vote
27
down vote
In the particular example you show, there is no difference. But imagine you would later on add two const
qualifier like the following:
const auto pp = p;
const auto *ppp = p;
Is it still the same? Turns out that this is identical to
int * const pp = p; // pointer is readonly
const int *ppp = p; // pointer is readonly
because in auto pp = p
, auto
matches int*
as a whole, and const
modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p
, auto
matches int
, and this is what const
applies to.
Because of this notable difference and because we should use const
variables whenever possible, I'd advise you to always use auto*
when using type deduction for pointer variables. There is no way to const
-qualify the pointer itself instead of the pointee, and if you want to const
-qualify both, this is possible by
const auto * const pppp = p;
which doesn't work without the *
.
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup'sTour
2nd ed. (I may have missed it.)
– davidbak
Nov 29 at 15:10
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
2
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
2
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
add a comment |
up vote
27
down vote
up vote
27
down vote
In the particular example you show, there is no difference. But imagine you would later on add two const
qualifier like the following:
const auto pp = p;
const auto *ppp = p;
Is it still the same? Turns out that this is identical to
int * const pp = p; // pointer is readonly
const int *ppp = p; // pointer is readonly
because in auto pp = p
, auto
matches int*
as a whole, and const
modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p
, auto
matches int
, and this is what const
applies to.
Because of this notable difference and because we should use const
variables whenever possible, I'd advise you to always use auto*
when using type deduction for pointer variables. There is no way to const
-qualify the pointer itself instead of the pointee, and if you want to const
-qualify both, this is possible by
const auto * const pppp = p;
which doesn't work without the *
.
In the particular example you show, there is no difference. But imagine you would later on add two const
qualifier like the following:
const auto pp = p;
const auto *ppp = p;
Is it still the same? Turns out that this is identical to
int * const pp = p; // pointer is readonly
const int *ppp = p; // pointer is readonly
because in auto pp = p
, auto
matches int*
as a whole, and const
modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p
, auto
matches int
, and this is what const
applies to.
Because of this notable difference and because we should use const
variables whenever possible, I'd advise you to always use auto*
when using type deduction for pointer variables. There is no way to const
-qualify the pointer itself instead of the pointee, and if you want to const
-qualify both, this is possible by
const auto * const pppp = p;
which doesn't work without the *
.
edited 21 hours ago
gsamaras
49.1k2398179
49.1k2398179
answered Nov 29 at 11:33
lubgr
9,99621644
9,99621644
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup'sTour
2nd ed. (I may have missed it.)
– davidbak
Nov 29 at 15:10
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
2
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
2
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
add a comment |
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup'sTour
2nd ed. (I may have missed it.)
– davidbak
Nov 29 at 15:10
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
2
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
2
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's
Tour
2nd ed. (I may have missed it.)– davidbak
Nov 29 at 15:10
Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's
Tour
2nd ed. (I may have missed it.)– davidbak
Nov 29 at 15:10
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
– Ruslan
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
@Ruslan You're right, I'll improve the wording.
– lubgr
Nov 29 at 15:13
2
2
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
@davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
– lubgr
Nov 29 at 15:14
2
2
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
– Scheff
Nov 30 at 9:12
add a comment |
up vote
7
down vote
There is no difference in auto
and auto *
in this particular case. In case of auto pp = p;
type will be deduced to int *
while in case of auto *ppp = p;
type will be deduced to int
.
auto
qualifier:
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]
Note that unlike auto
the auto *
will deduce only pointer types.
6
The key point isauto
uses template argument deduction rules, and soauto*
will only deduce pointer types.
– rustyx
Nov 29 at 10:58
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
add a comment |
up vote
7
down vote
There is no difference in auto
and auto *
in this particular case. In case of auto pp = p;
type will be deduced to int *
while in case of auto *ppp = p;
type will be deduced to int
.
auto
qualifier:
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]
Note that unlike auto
the auto *
will deduce only pointer types.
6
The key point isauto
uses template argument deduction rules, and soauto*
will only deduce pointer types.
– rustyx
Nov 29 at 10:58
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
add a comment |
up vote
7
down vote
up vote
7
down vote
There is no difference in auto
and auto *
in this particular case. In case of auto pp = p;
type will be deduced to int *
while in case of auto *ppp = p;
type will be deduced to int
.
auto
qualifier:
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]
Note that unlike auto
the auto *
will deduce only pointer types.
There is no difference in auto
and auto *
in this particular case. In case of auto pp = p;
type will be deduced to int *
while in case of auto *ppp = p;
type will be deduced to int
.
auto
qualifier:
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]
Note that unlike auto
the auto *
will deduce only pointer types.
edited Nov 29 at 17:49
answered Nov 29 at 10:52
haccks
85.5k20125217
85.5k20125217
6
The key point isauto
uses template argument deduction rules, and soauto*
will only deduce pointer types.
– rustyx
Nov 29 at 10:58
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
add a comment |
6
The key point isauto
uses template argument deduction rules, and soauto*
will only deduce pointer types.
– rustyx
Nov 29 at 10:58
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
6
6
The key point is
auto
uses template argument deduction rules, and so auto*
will only deduce pointer types.– rustyx
Nov 29 at 10:58
The key point is
auto
uses template argument deduction rules, and so auto*
will only deduce pointer types.– rustyx
Nov 29 at 10:58
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
@rustyx; I added particular case.
– haccks
Nov 29 at 11:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53537326%2fwhats-the-difference-between-auto-pp-and-auto-ppp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown