gpt4 book ai didi

c++ - 使用父成员的 decltype 不在 MSVC 上的 lambda 内部工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:12:24 33 4
gpt4 key购买 nike

以下代码可在 GCC、Clang 和 MSVC 上编译:

struct Foo {
int x;
};

struct Bar: Foo {
decltype(x) z;
};

但是这个不能用 MSVC 编译:

struct Foo {
int x;
};

auto m = [] () {
struct Bar: Foo {
decltype(x) z;
};
};

错误是:

error C4573: the usage of 'Foo::x' requires the compiler to capture 'this' but the current default capture mode does not allow it

这对我来说似乎错了...所以我想知道这是 MSVC 错误还是 gcc/clang 的扩展?

在所有情况下,是否有另一种方法可以做我想做的事而不必在 Bar< 中添加前缀 x (decltype(Foo::x))/?


注意:原来问题出现在写this answer的时候,我基本上想执行以下操作以避免必须在宏中生成名称:

auto something = [] () {
struct: Foo {
decltype(x) z;
auto operator()() { return z; }
} foo;
return foo.operator();
}();

目前我正在使用一个结构,所以我必须做类似的事情:

MACRO(Foo, x) my_var;
// or... MACRO(my_var, Foo, x);
auto value = my_var();

但我想:

auto value = MACRO(Foo, x);

最佳答案

这似乎是一个编译器错误。

It is VC++ 19.00.23506, the version on rextester.com

最新的 VC++ 编译器,版本号为 19.00.24213,已经修复了这个问题。

显然在 VC++ 的 lambda 中使用 decltype 带来了 some issues detecting type from out of scope .

显式地告诉编译器 Foo::x 可以解决这个问题,因为您已经知道了。

关于c++ - 使用父成员的 decltype 不在 MSVC 上的 lambda 内部工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036026/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com