如何检查一个成员函数是否有const过载? [英] How can I check whether a member function has const overload?

查看:147
本文介绍了如何检查一个成员函数是否有const过载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

  struct foo {
void ham(){}
()const {}
};

struct bar {
void ham(){}
};

假设我有一个模板函数,我可以告诉给定类型是否有一个const重载 ham

解决方案



(比较:@ Jarod42的方法检查确切的签名,返回类型+参数,另一个 void_t 表达式sfinae现在只检查 ham()是否可以被调用。)

此外,它还适用于目前的 MSVC 2015 Update 1 版本(与通常的 void_t )。

 模板< typename V,typename ... Args> 
struct is_callable_impl
{
template< typename C> static constexpr auto test(int) - > decltype(std :: declval< C>()。ham(std :: declval< Args>()...),bool {}){return true; }
template< typename> static constexpr auto test(...){return false; }
static constexpr bool value = test< V>(int {});
using type = std :: integral_constant< bool,value> ;;
};

template< typename ... Args>
using is_callable = typename is_callable_impl< Args ...> :: type;

使用

  struct foo 
{
void ham(){}
void ham()const {}
int ham(int)const {}
};

int main()
{
std :: cout
<< is_callable< foo> :: value // true
< ; is_callable< const foo> :: value // true
<< is_callable< const foo,int> :: value // true
<< is_callable< const foo,double> value //也为true,double被转换为int
<< is_callable< const foo,std :: string> :: value // false,不能调用foo :: ham(std :: string) const
<< std :: endl;
}

在Coliru上演示



我建议您查看 boost.hana


Lets say I have

struct foo {
    void ham() {}
    void ham() const {}
};

struct bar {
    void ham() {}
};

Assuming I have a templated function, can I tell whether given type has a const overload for ham?

解决方案

SFINAE over and over again. Here is another option which is unspecified about the return types but lets you specify the arguments.

(For comparison: the approach by @Jarod42 checks the exact signature, return type + arguments, the other void_t expression sfinae stuff up to now checks only whether ham() can be called.)

Plus, it works with the current MSVC 2015 Update 1 version (unlike the usual void_t stuff).

template<typename V, typename ... Args>
struct is_callable_impl
{
    template<typename C> static constexpr auto test(int) -> decltype(std::declval<C>().ham(std::declval<Args>() ...), bool{}) { return true; }
    template<typename> static constexpr auto test(...) { return false; }
    static constexpr bool value = test<V>(int{});
    using type = std::integral_constant<bool, value>;
};

template<typename ... Args>
using is_callable = typename is_callable_impl<Args...>::type;

Use it as

struct foo
{
    void ham() {}
    void ham() const {}
    int ham(int) const {}
};

int main()
{
     std::cout
      <<is_callable<foo>::value                //true
      <<is_callable<const foo>::value          //true
      <<is_callable<const foo, int>::value     //true
      <<is_callable<const foo, double>::value  //also true, double is converted to int
      <<is_callable<const foo, std::string>::value  //false, can't call foo::ham(std::string) const
      <<std::endl;
}

Demo on Coliru

For the "newest" sfinae stuff, however, I suggest you have a look at boost.hana.

这篇关于如何检查一个成员函数是否有const过载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆