[英]Using std::variant in constructor initialization list
I am trying to use std::variant
to generalize some of my code.我正在尝试使用std::variant
来概括我的一些代码。 However, I am running issues into when calling a constructor.但是,我在调用构造函数时遇到了问题。
I define a class TCPServer
as follows:我定义了一个类TCPServer
如下:
class TCPServer {
public:
TCPServer(aio::io_context &io_context, std::variant<PlainServer, PIRServer>);
std::variant<PlainServer, PIRServer> server_;
private:
...
};
And I define classes PIRServer
and PlainServer
as follows:我定义类PIRServer
和PlainServer
如下:
class PlainServer : public TCPServer {
public:
explicit PlainServer(aio::io_context& io_context);
...
private:
...
};
PlainServer::PlainServer(aio::io_context& io_context) :
server_config_(server_config),
TCPServer(io_context, this) {}
I omit PIRServer
because it doesn't contribute to the understanding of the problem.我省略了PIRServer
因为它无助于理解问题。
My IDE underlines the initialization of TCPServer
in the PlainServer
constructor and says: "No matching constructor for initialization of 'TCPServer".我的 IDE 在PlainServer
构造函数中强调了TCPServer
的初始化,并说:“没有匹配的构造函数用于初始化 'TCPServer”。 Am I using std::variant
incorrectly?我是否错误地使用了std::variant
?
Your variant
holds a PlainServer
.您的variant
包含一个PlainServer
。 this
is a pointer to a PlainServer
. this
是一个指向PlainServer
的指针。 You probably want:你可能想要:
class TCPServer {
public:
TCPServer(aio::io_context &io_context, std::variant<PlainServer*, PIRServer*>);
...
};
PlainServer::PlainServer(aio::io_context& io_context) :
server_config_(server_config),
TCPServer(io_context, std::variant<PlainServer*, PIRServer*>(this)) {}
The explicit construction of the variant is required because its single arg constructors are explicit
.变体的显式构造是必需的,因为它的单个 arg 构造函数是explicit
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.