The following code works with gcc
#include <map>
int main() {
std::map<int, double> dict;
const auto lambda = [&]()
{
decltype(dict)::value_type bar;
};
}
But for msvc I have to additionally use std::remove_reference
#include <map>
#include <type_traits>
int main() {
std::map<int, double> dict;
const auto lambda = [&]()
{
std::remove_reference_t<decltype(dict)>::value_type bar;
};
}
Otherwise I get an error:
error C2651: 'std::map<int,double,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &': left of '::' must be a class, struct or union
Which compiler shows the correct behaviour according to the standard?
update:
For msvc decltype(dict)
really is a reference, as the following code
#include <map>
int main()
{
std::map<int, double> dict;
const auto lambda = [&]()
{
decltype(dict) foo;
};
}
errors with
error C2530: 'foo': references must be initialized
If this really is wrong behaviour, it could lead to nasty bugs, like dangling references when code is compiled with msvc.
#include <map>
std::map<int, double> return_a_map()
{
std::map<int, double> result;
return result;
}
int main()
{
std::map<int, double> dict;
const auto lambda = [&]()
{
decltype(dict) foo = return_a_map();
// foo is a dangling reference in msvc
};
}