gpt4 book ai didi

C++ 简化构造函数重载

转载 作者:行者123 更新时间:2023-11-28 00:17:18 25 4
gpt4 key购买 nike

假设我正在创建一个类 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 个以上的变量要排列,这会很快变得丑陋。

最佳答案

模板怎么样:

#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))
{ }

// ...
};

关于C++ 简化构造函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com