I am attempting to create class ServerLogic
, where constructor gets different parameters based on the template argument and processes it in different ways (in the case it passes them to endpoint
constructor)
// Shortened version of code
using namespace boost::asio;
template <typename Protocol>
class ServerLogic
{
static_assert(
std::is_same_v<Protocol, local::stream_protocol> ||
std::is_same_v<Protocol, ip::tcp>
); // Allow using only either `local::stream_protocol` or `local::tcp` as Protocol
Protocol::endpoint *ep;
public:
ServerLogic(std::string sock_path)
requires std::is_same<Protocol, local::stream_protocol>::value
: ep(new Protocol::endpoint(sock_path))
{} // If Protocol is `local::stream_protocol` then create ep of
// `local::stream_protocol` due to it's constructor
ServerLogic(const ip::address &addr, unsigned short port)
requires std::is_same<Protocol, ip::tcp>::value
: ep(new Protocol::endpoint(addr, port))
{} // If Protocol is `ip::tcp` then create ep of `ip::tcp` protocol due to
// it's constructor, different from `local::stream_protocol::endpoint` constructor
But compiler puts the errors:
[build] /home/shkiperdesna/cppbot/src/ServerLogic.hpp: In instantiation of ‘ServerLogic<Protocol>::ServerLogic(const boost::asio::ip::address&, short unsigned int) requires is_same_v<Protocol, boost::asio::ip::tcp> [with Protocol = boost::asio::local::stream_protocol]’:
[build] /home/shkiperdesna/cppbot/src/ServerLogic.cpp:95:16: required from here
[build] /home/shkiperdesna/cppbot/src/ServerLogic.hpp:49:14: error: no matching function for call to ‘boost::asio::local::basic_endpoint<boost::asio::local::stream_protocol>::basic_endpoint(const boost::asio::ip::address&, short unsigned int&)’
...
I understand that despite using requires
keyword, the compiler tries to compile both constructors with both protocols. How to fix that and split constructors for different template arguments?