“Manual” signature overload resolution

左心房为你撑大大i 提交于 2019-11-28 21:56:56
pal

i'm sure it is doable your way, but may be you will be satisfied with this one https://gist.github.com/dabrahams/3779345

template<class...Fs> struct overloaded;

template<class F1, class...Fs>
struct overloaded<F1, Fs...> : F1, overloaded<Fs...>::type
{
typedef overloaded type;

overloaded(F1 head, Fs...tail)
: F1(head),
overloaded<Fs...>::type(tail...)
{}
using F1::operator();
using overloaded<Fs...>::type::operator();
};

template<class F>
struct overloaded<F> : F
{
typedef F type;
using F::operator();
};

template<class...Fs>
typename overloaded<Fs...>::type overload(Fs...x)
{ return overloaded<Fs...>(x...); }

auto f = overload(
[](int x) { return x+1; },
[](char const* y) { return y + 1; },
[](int* y) { return y; });

I think you can use something like these traits... But if you want make overloading resolution fully as in standard - you need more code http://en.cppreference.com/w/cpp/language/implicit_cast

#include <type_traits>

template<typename T, typename D>
struct is_constructible
{
   template<typename C, typename F>
   static auto test(C*) -> decltype(C(std::declval<F>()), std::true_type());
   template<typename, typename>
   static std::false_type test(...);
   static const bool value = std::is_class<T>::value && 
      std::is_same<std::true_type, decltype(test<T, D>(0))>::value;
};

template<typename T, typename D>
struct has_conversion_operator
{
   static std::true_type test(D d);
   template<typename C, typename F>
   static auto test(C* c) -> decltype(test(*c));
   template<typename, typename>
   static std::false_type test(...);

   static const bool value = std::is_class<T>::value && 
      !is_constructible<T, D>::value && 
      std::is_same<std::true_type, decltype(test<T, D>(0))>::value;
};

template<typename T, typename D>
struct is_standard_convertible : 
   std::integral_constant<bool, !has_conversion_operator<T, D>::value && 
   !is_constructible<T, D>::value &&
   std::is_convertible<T, D>::value>
{
};

template<typename T, typename D>
struct is_user_convertible :
   std::integral_constant<bool, has_conversion_operator<T, D>::value || 
   is_constructible<T, D>::value>
{
};

and implement what you want like: first check, that signatures are standard_convertible if not check that signature are user_convertible.

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