Distinguish between types using SFINAE and void_t

独自空忆成欢 提交于 2019-12-10 23:16:04

问题


I faced some situation where I have to write two functions, one of them should be invoked with primitive types and std::string. The other one should be called with other types.

So far I ended with working solution:

template <typename...>
struct Void_t_helper {
    using type = void;
};

template <typename... Ts>
using Void_t = typename Void_t_helper<Ts...>::type;


template <typename T, typename = void>
struct Is_string : std::false_type {};

template <typename T> 
struct Is_string<T, Void_t<decltype (std::declval<T> ().c_str ())>> : std::is_same<decltype (std::declval<T> ().c_str ()), const char*>::type {};


template <typename T>
std::enable_if_t<Is_string<T>::value || std::is_arithmetic<T>::value, void> foo (T) {
    std::cout << "string or primitive\n";
}

template <typename T>
std::enable_if_t<!Is_string<T>::value && !std::is_arithmetic<T>::value, void> foo (T) {
    std::cout << "other type\n";
}

And the usage:

foo (1);
foo (1.2);
foo (std::string {"fsdf"});
foo (std::vector<int> {1, 2, 3});
foo (std::vector<std::string> {"a", "v", "c"});

produces as expected:

string or primitive
string or primitive
string or primitive
other type
other type

My question is: Do you know better solution to this kind of problem?

I am not really sure if checking if c_str() exists is the better option I can get. I am aware that I could probably write some wrapper class that for primitive types and std::string would have some category_t defined with value X, and for other types value Y and distinguish between these groups using this category, but still I think that c_str() checking is more convenient.


回答1:


I am not really sure if checking if c_str() exists is the better option I can get.

Ideally you'll be checking for what you actually want.

That can either be a set of known types or templates, or it can be a concept.

At the moment, you're checking for "the concept of having a c_str() member function which returns a pointer to constant chars".

The question is, what concept does your SFINAE'd function need?

If it will use the c_str() member, that's reasonable. But if it's going to use other members or types of the string, you probably want to build a compound concept to describe the parts of the interface you're going to exercise.

Of course, you may just want to confirm that it is actually a specialisation of std::string. It's difficult (impossible) to tell unless you state the use case.



来源:https://stackoverflow.com/questions/46756319/distinguish-between-types-using-sfinae-and-void-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!