相关疑难解决方法(0)

lambda中引用捕获对象的类型

以下代码适用于gcc

#include <map>

int main() {
    std::map<int, double> dict;
    const auto lambda = [&]()
    {
        decltype(dict)::value_type bar;
    };
}
Run Code Online (Sandbox Code Playgroud)

但对于msvc我还要另外使用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;
    };
}
Run Code Online (Sandbox Code Playgroud)

否则我收到一个错误:

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
Run Code Online (Sandbox Code Playgroud)

哪个编译器根据标准显示正确的行为?

更新:

对于msvc decltype(dict)确实是一个参考,如下面的代码

#include <map>

int main()
{
    std::map<int, double> dict;
    const auto lambda = …
Run Code Online (Sandbox Code Playgroud)

c++ lambda decltype language-lawyer

17
推荐指数
1
解决办法
412
查看次数

标签 统计

c++ ×1

decltype ×1

lambda ×1

language-lawyer ×1