I am trying to use std::variant
to generalize some of my code. However, I am running issues into when calling a constructor.
I define a class TCPServer
as follows:
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:
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.
My IDE underlines the initialization of TCPServer
in the PlainServer
constructor and says: "No matching constructor for initialization of 'TCPServer". Am I using std::variant
incorrectly?
Your variant
holds a PlainServer
. this
is a pointer to a 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
.