Reputation: 41
This code give me strange debug info in visual studio 2015
int main() {
const int i = 42;
auto j = i; const auto &k = i; auto *p = &i;
const auto j2 = i, &k2 = i;
}
The resulting types were:
&k = const int &
&k2 = const int *
I think those should both be const int &
.
Question is, why is my Visual Studio Debugger saying &k
and &k2
are of different type?
Upvotes: 4
Views: 3263
Reputation: 234715
k
and k2
are both const int&
types.
Here is the full type list. Note that top-level const
is discarded for auto
type deduction.
int main()
{
const int i = 42;
auto j = i; // i is an int (const is top-level)
const auto &k = i; // k is a const int&
auto *p = &i; // p is a const int* (const persists as not top-level).
const auto j2 = i, &k2 = i; // j2 is a const int, k2 is a const int&
}
Finally, if you had written
auto q = &k2;
then the type of q
is a const int*
, since the const
is not top-level so is not discarded but auto
type deduction. This recovers the debug info you observe.
j2
and k2
look dissimilar but really that's due to how declarations work with the comma, cf. The confusion can be unpicked by writing
const int j2 = i, &k2 = i;
You can always find out for sure using the C++11 standard library function is_same
:
e.g.
bool am_I_the_same = std::is_same<decltype(k2), const int&)::value
Where decltype
recovers the type of its argument.
Reference: http://en.cppreference.com/w/cpp/types/is_same
Upvotes: 1