我尝试了迈向模板的第一步。我有一个使用boost / property_tree读取配置参数的类。目前我有几十个相同的吸气剂。例如:
inline std::string getSocket(void) {
return any_cast<std::string>(param["socket"]);
}
现在我尝试创建一个模板:
声明:
template <typename T, typename R>
R getValue(const std::string &);
定义:
template <typename T=std::string, typename R=std::string>
R MilterCfg::getValue(const std::string &key) {
if (param.count(key) == 0)
return "";
return any_cast<T>(param[key]);
}
调用另一个cpp文件:
mfsocket = ::config->getValue<>("socket");
编译器不接受这个:
/Users/croessner/ClionProjects/sigh/src/milter.cpp:491:30: error: no matching member function for call to 'getValue'
mfsocket = ::config->getValue<>("socket");
~~~~~~~~~~^~~~~~~~~~
/Users/croessner/ClionProjects/sigh/src/config.h:112:11: note: candidate template ignored: couldn't infer template argument 'T'
R getValue(const std::string &);
^
我使用clang ++编译器在Mac OS X El-Capitan上尝试此操作。我想我真的很想念模板。但是我在这里错过了什么?
这是完整的标题:
#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_
#include <map>
#include <string>
#include <boost/any.hpp>
#include <boost/program_options/variables_map.hpp>
namespace po = boost::program_options;
extern bool debug;
namespace conf {
using boost::any_cast;
typedef std::map<std::string, boost::any> config_t;
/*!
* \brief Read a configuration file and store settings
*
* All milter settings may be stored in a configuration file. This class
* reads a default configuration file, if not given as command line
* argument and extracts all keys and values. For each key that is not
* found, but requested my the milter, a default value is defined in a
* local data structure.
*/
class MilterCfg {
public:
MilterCfg(const po::variables_map &);
virtual ~MilterCfg() = default;
/*!
* \brief The milter socket
*
* The socket may have one of three formats. First is
* inet:portnumber\@host, second is inet6:portnumber\@host6 or a unix
* socket like unix:/pat/to/socket. host and host6 may be a hostname
* or a valid IP address. IPv6 addresses must be written in squared
* braces.
*/
inline std::string getSocket(void) {
return any_cast<std::string>(param["socket"]);
}
/*!
* The milter will drop its privileges to this user
*/
inline std::string getUser(void) {
return any_cast<std::string>(param["user"]);
}
/*!
* The milter will drop its privileges to this group
*/
inline std::string getGroup(void) {
return any_cast<std::string>(param["group"]);
}
/*!
* \brief An optional PID file
*
* If desired, a PID file may be created on startup. It will be
* automatically removed, when the milter gets stopped again.
*/
inline std::string getPidFile(void) {
return any_cast<std::string>(param["pidfile"]);
}
/*!
* \brief Map file containing S/MIME certificates
*
* This file contains a mapping between email addresses and
* associated S/MIME certificates and keys.
*/
inline std::string getMapFile(void) {
return any_cast<std::string>(param["mapfile"]);
}
/*!
* \brief Path to a temporary directory
*
*/
inline std::string getTmpDir(void) {
return any_cast<std::string>(param["tmpdir"]);
}
#if !__APPLE__ && !defined _NOT_DAEMONIZE
/*!
* \brief Bring the milter to background
*
* The milter gets a daemon. The root path is set to '/' and the
* standard in and out channels are closed
*/
inline bool getDaemon(void) {
return any_cast<bool>(param["daemon"]);
}
#endif // !__APPLE__ && !defined _NOT_DAEMONIZE
template <typename T, typename R>
R getValue(const std::string &);
private:
/*!
* \brief Data store for configuration settings
*/
config_t param;
/*!
* \brief Default settings for the milter
*
* If a required setting could not be read from the configuration, a
* default setting will be used from this data structure.
*/
struct {
//! \brief Milter socket
std::string socket = "inet:4000@127.0.0.1";
//! \brief Milter system user
std::string user = "milter";
//! \brief Milter system group
std::string group = "milter";
//! \brief Optional PID file
std::string pidfile = std::string();
#if !__APPLE__ && !defined _NOT_DAEMONIZE
//! \brief Run the milter as a daemon process
bool daemon = false;
#endif // !__APPLE__ && !defined _NOT_DAEMONIZE
//! \brief Location for the map file
std::string mapfile = std::string();
//! \brief Location for temporary files
std::string tmpdir = "/tmp";
} defaults;
};
} // namespace conf
#endif // SRC_CONFIG_H_
和cpp文件:
#include "config.h"
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
namespace fs = boost::filesystem;
namespace conf {
using boost::any_cast;
MilterCfg::MilterCfg(const po::variables_map &vm) {
std::string conffile = vm["config"].as<std::string>();
boost::property_tree::ptree pt;
try {
if (fs::exists(fs::path(conffile))
&& fs::is_regular(fs::path(conffile))) {
boost::property_tree::ini_parser::read_ini(conffile, pt);
} else {
std::cerr << "Error: Unable to read config file "
<< conffile << std::endl;
}
}
catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
try {
param["socket"] = pt.get<std::string>("Milter.socket");
}
catch (...) {
param["socket"] = defaults.socket;
}
try {
param["user"] = pt.get<std::string>("Milter.user");
}
catch (...) {
param["user"] = defaults.user;
}
try {
param["group"] = pt.get<std::string>("Milter.group");
}
catch (...) {
param["group"] = defaults.group;
}
try {
param["pidfile"] = pt.get<std::string>("Milter.pidfile");
}
catch (...) {
param["pidfile"] = defaults.pidfile;
}
try {
param["mapfile"] = pt.get<std::string>("Milter.mapfile");
}
catch (...) {
param["mapfile"] = defaults.mapfile;
}
try {
param["tmpdir"] = pt.get<std::string>("Milter.tmpdir");
}
catch (...) {
param["tmpdir"] = defaults.tmpdir;
}
#if !__APPLE__ && !defined _NOT_DAEMONIZE
try {
param["daemon"] = pt.get<bool>("Milter.daemon");
}
catch (...) {
param["daemon"] = defaults.daemon;
}
#endif // !__APPLE__ && !defined _NOT_DAEMONIZE
if (::debug) {
std::cout << "Configuration file values:" << std::endl;
std::cout << "user="
<< any_cast<std::string>(param["user"])
<< std::endl;
std::cout << "group="
<< any_cast<std::string>(param["group"])
<< std::endl;
std::cout << "socket="
<< any_cast<std::string>(param["socket"])
<< std::endl;
std::cout << "pidfile="
<< any_cast<std::string>(param["pidfile"])
<< std::endl;
#if !__APPLE__ && !defined _NOT_DAEMONIZE
std::cout << "daemon="
<< std::boolalpha << any_cast<bool>(param["daemon"])
<< std::endl;
#endif // !__APPLE__ && !defined _NOT_DAEMONIZE
std::cout << "mapfile="
<< any_cast<std::string>(param["mapfile"])
<< std::endl;
std::cout << "tmpdir="
<< any_cast<std::string>(param["tmpdir"])
<< std::endl;
}
}
template <typename T=std::string, typename R=std::string>
R MilterCfg::getValue(const std::string &key) {
if (param.count(key) == 0)
return "";
return any_cast<T>(param[key]);
}
} // namespace conf
所以你看,很多重复的逻辑。我的感觉是,这可以做得更通用。你能给我一个提示吗?提前致谢
答案 0 :(得分:4)
默认参数取决于声明,而不是定义:
// declaration
template <typename T=std::string, typename R=std::string>
R getValue(const std::string &);
// definition
template <typename T, typename R>
R MilterCfg::getValue(const std::string &key) {
if (param.count(key) == 0)
return "";
return any_cast<T>(param[key]);
}
N.b。允许呼叫站点的尖括号但是多余的;以下是更惯用的:
mfsocket = ::config->getValue("socket");