说我正在上课client
。我希望client
能够使用以下类型构建:
client(const boost::network::uri::uri &, const boost::network::uri::uri &)
client(const std::string &, const std::string &)
client(const char *, const char *)
但是......我也想要所有的排列......
client(const boost::network::uri::uri &, const boost::network::uri::uri &)
client(const std::string &, const std::string &)
client(const char * &, const char * &)
client(const boost::network::uri::uri &, const std::string &)
client(const std::string &, const boost::network::uri::uri &)
client(const boost::network::uri::uri &, const char * &)
client(const char * &, const boost::network::uri::uri &)
client(const std::string &, const char * &)
client(const char * &, const std::string &)
可以假设我的客户端类,为简单起见,如下所示。
#include <string>
#include <boost/network.hpp>
#define HOST_URI "..."
#define AUTH_URI HOST_URI"..."
namespace bn = boost::network;
class client
{
private:
const bn::uri::uri host_;
const bn::uri::uri auth_;
public:
client(const bn::uri::uri & host = const bn::uri::uri(HOST_URI),
const bn::uri::uri & auth = const bn::uri::uri(AUTH_URI));
client(const std::string & host = const std::string(HOST_URI),
const std::string & auth = const std::string(AUTH_URI));
client(const char * & host = HOST_URI,
const char * & auth = AUTH_URI);
client(const bn::uri::uri & host = const bn::uri::uri(HOST_URI),
const std::string & auth = const std::string(AUTH_URI));
client(const std::string & host = const std::string(HOST_URI),
const bn::uri::uri & auth = const bn::uri::uri(AUTH_URI));
client(const bn::uri::uri & host = const bn::uri::uri(HOST_URI),
const char * & auth = AUTH_URI);
client(const char * & host = HOST_URI,
const bn::uri::uri & auth = const bn::uri::uri(AUTH_URI));
client(const std::string && host = const std::string(HOST_URI),
const char * & auth = AUTH_URI);
client(const char * & host = HOST_URI,
const std::string && auth = const std::string(AUTH_URI));
};
目前定义为:
#include <string>
#include <boost/network.hpp>
namespace bn = boost::network;
client::client(const bn::uri::uri & host,
const bn::uri::uri & auth)
: host_(host), auth_(auth)
{
...
};
client::client(const std::string & host,
const std::string & auth)
: client(bn::uri::uri(host), bn::uri::uri(auth)){}
client::client(const char * & host,
const char * & auth)
: client(bn::uri::uri(host), bn::uri::uri(auth)){}
client::client(const bn::uri::uri & host,
const std::string & auth)
: client(host, bn::uri::uri(auth)){}
client::client(const std::string & host,
const bn::uri::uri & auth)
: client(bn::uri::uri(host), auth){}
client::client(const bn::uri::uri & host,
const char * & auth)
: client(host, bn::uri::uri(auth)){}
client::client(const char * & host,
const bn::uri::uri & auth)
: client(bn::uri::uri(host), auth){}
client::client(const std::string & host,
const char * & auth)
: client(bn::uri::uri(host), bn::uri::uri(auth)){}
client::client(const char * & host,
const std::string & auth)
: client(bn::uri::uri(host), bn::uri::uri(auth)){}
所以我的问题是,这样做的正确和简单方法是什么? 当然,我这次手动完成了所有的排列,但是将来我可以有3个以上的变量进行置换,这会变得很难看,很快。
答案 0 :(得分:1)
由于uri定义了可以使用string
以及const char *
的构造函数,因此请删除其参数不包含uri类型的构造函数。这使得uri的user-defined conversion可以隐式转换这些类型。
#define HOST_URI "..."
#define AUTH_URI HOST_URI"..."
class client
{
private:
uri host_;
uri auth_;
public:
client(const uri & host = uri(HOST_URI),
const uri & auth = uri(AUTH_URI));
client(const char * host = HOST_URI,
const char * auth = AUTH_URI);
};
-
client::client(const uri::uri & host,
const uri::uri & auth)
: host_(host), auth_(auth)
{
...
};
client::client(const char * host,
const char * auth)
: client(uri::uri(host), uri::uri(auth)){}
&#34;我如何具体地将它委托给那个构造函数?&#34; 在使用委托构造函数时要明确。
client(const char * && host, const char * && auth) :
client(uri(host), uri(auth)){}
或
client(const char * && host, const char * && auth) :
client(string(host), string(auth)){}
答案 1 :(得分:1)
模板怎么样:
#include <type_traits>
class client
{
uri host_;
uri auth_;
public:
template <typename U, typename V,
typename = typename std::enable_if<
std::is_constructible<uri, U&&>::value &&
std::is_constructible<uri, V&&>::value>::type>
client(U && u, V && v)
: host_(std::forward<U>(u))
, auth_(std::forward<V>(v))
{ }
// ...
};
答案 2 :(得分:1)
你可以创建一个可以将所有这三件事作为参数的类。你的帖子没有包含足够的细节给我写一个具体的答案,但这里是伪代码:
struct input_helper
{
input_helper(uri &u);
input_helper(string &u);
input_helper(char *u);
// data members to hold the inputs, maybe other processing to bring them to a common type
};
// the constructor
client(input_helper host, input_helper auth);