通过Boost的Property树的.get函数自动将c风格的字符串解释为std :: string

时间:2016-06-15 19:21:04

标签: string c++11 boost boost-propertytree

我使用boosts属性树,包含在

#include "boost\property_tree\ptree.hpp"

并且......我想创建一个简单的函数来代替一个值,以防通过相当直接的模板函数找不到:

template <typename Type>
Type getValueOrDefault( std::string const& str, Type defaultValue )
{
    Type returnValue = defaultValue;

    try {
        returnValue = mSettings.get<Type>( str );
    }
    catch ( boost::property_tree::ptree_error &e )
    {
        // Log error!
    }

    return returnValue;
}

原则上这很有效,但如果我依赖C风格的字符串会遇到一些问题。例如,按如下方式调用函数:

getValueOrDefault( "pathToImportantStuffParameter", "c:/defaultdir/" )

将导致以下错误:

boost\property_tree\stream_translator.hpp(36): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<char,std::char_traits<char>>' (or there is no acceptable conversion)

错误源于将char const *作为模板参数传递,这有点道理。这个问题的两个明显的解决方案是强制默认值为std :: string对象,如下所示:

getValueOrDefault<std::string>( "pathToImportantStuffParameter", "c:/defaultdir/" )
getValueOrDefault( "pathToImportantStuffParameter", std::string("c:/defaultdir/") )

但是我想知道是否有人可能知道我可以使用的一些模板魔法来自动将c风格的字符串解释为std :: strings?

2 个答案:

答案 0 :(得分:2)

您可以提供char数组重载,将char数组转换为std::string,然后调用默认实现:

#include <iostream>
#include <string>

template <typename T>
T getValueOrDefault(const std::string& str, T&& defaultValue)
{
    std::cout << "inside default implementation" << std::endl;
    /* ... */
    return defaultValue;
}

template <std::size_t N>
std::string getValueOrDefault(const std::string& str, const char (&defaultValue)[N])
{
    std::cout << "inside char[] overload" << std::endl;
    return getValueOrDefault(str, std::string(defaultValue));
}


int main()
{
    auto x = getValueOrDefault("foo", "bar");
    return 0;
}

live example

另一种解决方案是使用自定义类型特征:

#include <string>
#include <type_traits>

template <typename T>
struct return_type
{
    using type = T;
};

template <>
struct return_type<const char*>
{
    using type = std::string;
};

template <typename T>
using return_type_t = typename return_type<typename std::decay<T>::type>::type;

template <typename T>
return_type_t<T> getValueOrDefault(const std::string& str, T&& defaultValue)
{
    return_type_t<T> value(defaultValue);
    /* ... */
    return value;
}


int main()
{
    auto x = getValueOrDefault("foo", "bar");
    static_assert(std::is_same<decltype(x), std::string>::value, "");
    return 0;
}

live example

答案 1 :(得分:1)

我找到的唯一方法是为getValueOrDefault专门化const char*getValueOrDefault明确地调用std::string

//Note that the return value is unspecified, it returns a 'const char*' to a temporary,
//which will be destroyed when the function returns
template <>
const char* getValueOrDefault(std::string const& str, const char* defaultValue)
{
    return getValueOrDefault<std::string>(str, defaultValue).c_str();
}

如果您希望该函数返回std::string而不是无效const char*,则必须稍微更改模板签名:

//Default return type is the same as paramter
template <typename Type, typename Return = Type>
Return getValueOrDefault(std::string const& str, Type defaultValue)
{
    //...
}

//Trick the compiler to select this overload for 'const char*'
template <typename Return = std::string>
Return getValueOrDefault(std::string const& str, const char* defaultValue)
{
    return getValueOrDefault<std::string, std::string>(str, defaultValue);
}

或者你可以简单地重载该函数(感谢@ m.s。)

//Overload for 'const char*'
std::string getValueOrDefault(std::string const& str, const char* defaultValue)
{
    return getValueOrDefault<std::string>(str, defaultValue);
}

还有第三种方法(如果你可以使用C ++ 14),使用字符串文字""s

//"c:/defaultdir/"s is a std::string (note the s after it => string literal)
getValueOrDefault("pathToImportantStuffParameter", "c:/defaultdir/"s);