gpt4 book ai didi

C++ 匿名结构作为 std::map 值

转载 作者:行者123 更新时间:2023-12-02 00:29:16 40 4
gpt4 key购买 nike

我正在使用 Visual Studio 2019 std:c++ 17,它支持匿名结构,例如

struct S 
{
struct { int i; };
};

但是,以下代码存在编译错误。

map<int, struct { int i; }> m;

我可以使用匿名结构作为 std::map 的值类型吗?

最佳答案

你不能直接做到这一点,但也可以间接做到

struct { int i; } s;
std::map<int, decltype(s)> a;

using MyType = struct { int i; };
std::map<int, MyType> b;

但请注意,ab 的类型不同。 decltype(s)MyType 的类型不同。

关于C++ 匿名结构作为 std::map 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351117/

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