从 boost::program_options 值中键入取消擦除
Type un-erasure from a boost::program_options value
我正在尝试使用 boost::program_options
改进我的程序。除其他事项外,我编写了一个无法编译的函数。这是一个编译失败的最小 .cpp 文件:
#include <boost/program_options.hpp>
template <typename C, typename E>
inline bool contains(const C& container, const E& element) {
return container.find(element) != container.end();
}
template <typename K, typename V>
V& updateFromConfig(V& updatee, const K& key, const po::variables_map& vm) {
if (contains(vm, key)) {
// option 1
updatee = vm[key];
// option 2
//updatee = vm[key].variable_value();
// option 3
// updatee = vm[key].as<V>();
// option 4
// updatee = vm[key].as();
}
return updatee;
}
template size_t& updateFromConfig<char*,size_t>(size_t& updatee, char* const& key, const po::variables_map& vm);
使用四个选项中的任何一个,我都会得到不同的编译器错误:
选项 1:
cannot convert ‘const boost::program_options::variable_value’ to ‘long unsigned int’ in assignment
updatee = vm[key];
选项 2:
invalid use of ‘boost::program_options::variable_value::variable_value’
选项 3:
18:25: error: expected primary-expression before ‘>’ token
18:27: error: expected primary-expression before ‘)’ token
选项 4:
no matching function for call to ‘boost::program_options::variable_value::as() const
我做错了什么?
C++语法为赢。 Do:
updatee = vm[key].template as<V>();
我正在尝试使用 boost::program_options
改进我的程序。除其他事项外,我编写了一个无法编译的函数。这是一个编译失败的最小 .cpp 文件:
#include <boost/program_options.hpp>
template <typename C, typename E>
inline bool contains(const C& container, const E& element) {
return container.find(element) != container.end();
}
template <typename K, typename V>
V& updateFromConfig(V& updatee, const K& key, const po::variables_map& vm) {
if (contains(vm, key)) {
// option 1
updatee = vm[key];
// option 2
//updatee = vm[key].variable_value();
// option 3
// updatee = vm[key].as<V>();
// option 4
// updatee = vm[key].as();
}
return updatee;
}
template size_t& updateFromConfig<char*,size_t>(size_t& updatee, char* const& key, const po::variables_map& vm);
使用四个选项中的任何一个,我都会得到不同的编译器错误:
选项 1:
cannot convert ‘const boost::program_options::variable_value’ to ‘long unsigned int’ in assignment updatee = vm[key];
选项 2:
invalid use of ‘boost::program_options::variable_value::variable_value’
选项 3:
18:25: error: expected primary-expression before ‘>’ token 18:27: error: expected primary-expression before ‘)’ token
选项 4:
no matching function for call to ‘boost::program_options::variable_value::as() const
我做错了什么?
C++语法为赢。 Do:
updatee = vm[key].template as<V>();