Determining return type based on input parameter

I am trying to implement a general configuration file parser, and I am wondering how to write a method in my class that can determine its return type, depending on the type of input parameter. Here is what I mean:

class Config
{
    ...
    template <typename T>
    T GetData (const std::string &key, const T &defaultValue) const;
    ...
}

To call the above method, I should use something like this:

some_type data = Config::GetData<some_type>("some_key", defaultValue);

How can I get rid of redundant specs? I saw that boost :: property_tree :: ptree :: get () is able to accomplish this trick, but the implementation is rather complicated, and I could not decrypt this complex declaration:

template<class Type, class Translator>
typename boost::enable_if<detail::is_translator<Translator>, Type>::type
get(const path_type &path, Translator tr) const;

If possible, I would like to do this without creating a dependency dependency on the code that my Config class will use.

PS: I am n00b when it comes to C ++ templates :(

+2
1

enable_if . , :

some_type data = Config::GetData("some_key", defaultValue);

, ++ 11 , :

auto data = Config::GetData("some_key", defaultValue);

... , ++ , . :

class Config {
    template <typename T>
    static T GetData(const std::string &key) const;
}
some_type data = Config::GetData("some_key");

, , , , . .

+4

Source: https://habr.com/ru/post/1682773/


All Articles