0

我试图让它工作:

template<class Type>
typename boost::enable_if< boost::mpl::or_<
boost::is_arithmetic<Type>,
is_string<Type> > >::type
get(const std::string &argPath, const Type &argDefault) {
    bool caught = false;
    std::stringstream ss;
    Type value;

    try {
        value = ptree_.get<Type>(argPath);
    } catch(ptree_bad_path &e) {
        caught = true;
    }

    if(caught)
        value = argDefault;

    ss << value;
    parameters_.insert(std::pair<std::string, std::string>(argPath, ss.str()));
    return value;
}

我使用了以下 is_string 类型特征:字符串的类型特征

我的目标是将我限制Type为字符串或算术类型,以便我可以将其推送到我的stringstream.

所以这个构建,但是当我尝试使用它时,它返回以下错误:

错误:应忽略的无效值

在成员函数'typename boost::enable_if, is_string, mpl_::bool_, mpl_::bool_, mpl_::bool_ >, void>::type FooClass::get(const std::string&, const Type&) [with Type = uint8_t]'</p>

错误:带有值的返回语句,在返回“void”的函数中

这是我尝试使用它的方法:

FooClass f;
item_value = f.get("tag1.tag2.item", DEFAULT_ITEM_VALUE);

任何帮助表示赞赏,在此先感谢!

4

1 回答 1

4

http://www.boost.org/doc/libs/1_53_0/libs/utility/enable_if.htmlenable_if有第二个参数默认为 void:

template <bool B, class T = void>
struct enable_if_c {
  typedef T type;
};

在我看来,您需要在 enable_if 中包含返回类型。(现在默认为无效。)

template<class Type>
typename boost::enable_if< boost::mpl::or_<
    boost::is_arithmetic<Type>,
    is_string<Type> >,
Type >::type
get(const std::string &argPath, const Type &argDefault);
于 2013-05-22T08:29:11.867 回答