typ des Referenzobjekts in Lambda
Der folgende Code funktioniert mit gcc
#include <map>
int main() {
std::map<int, double> dict;
const auto lambda = [&]()
{
decltype(dict)::value_type bar;
};
}
Aber für msvc Ich muss zusätzlich @ verwendstd::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;
};
}
nsonsten bekomme ichein Fehle:
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
Welcher Compiler zeigt das korrekte Verhalten gemäß dem Standard?
aktualisieren
Zum msvc decltype(dict)
ist wirklich eine Referenz, da der folgende Code
#include <map>
int main()
{
std::map<int, double> dict;
const auto lambda = [&]()
{
decltype(dict) foo;
};
}
Fehler mit
error C2530: 'foo': references must be initialized
Wenn dies wirklich ein falsches Verhalten ist, kann dies zu @ führe Gymnastikbugs, wie baumelnde Referenzen Wenn Code mit msvc kompiliert wird.
#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
};
}