我已经获得了带有std::queue
的C ++ 17代码,其中std::tuple
由指针和std::variant
组成。代码如下:
struct EventA
{
int a;
float b;
bool c;
};
struct EventB
{
char x;
unsigned int y;
};
class Window;
std::queue<std::tuple<Window *, std::variant<EventA, EventB>>> eventQueue;
使用std::variant
就地建立队列中新元素的正确语法是什么?我尝试了很多语法而没有成功:
eventQueue.emplace(nullptr, -1024, 2.0f, true);
eventQueue.emplace(nullptr, 'a', 2048u);
eventQueue.emplace(nullptr, {-1024, 2.0f, true});
eventQueue.emplace(nullptr, {'a', 2048u});
eventQueue.emplace(nullptr, {std::in_place_type<EventA>, -1024, 2.0f, true});
eventQueue.emplace(nullptr, {std::in_place_type<EventB>, 'a', 2048u});
eventQueue.emplace(nullptr, std::foward_as_tuple(std::in_place_type<EventA>, -1024, 2.0f, true));
eventQueue.emplace(nullptr, std::foward_as_tuple(std::in_place_type<EventB>, 'a', 2048u));