使用不同的enable_if条件选择成员函数 [英] Selecting a member function using different enable_if conditions

查看:227
本文介绍了使用不同的enable_if条件选择成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据类模板参数来确定调用成员函数的哪个版本。我试过这个:

  #include< iostream> 
#include< type_traits>

template< typename T>
struct Point
{
void MyFunction(typename std :: enable_if< std :: is_same< T,int> :: value,T> :: type * = 0)
{
std :: cout<< T是int。 << std :: endl;
}

void MyFunction(typename std :: enable_if<!std :: is_same< T,int> :: value,float> :: type * = 0)
{
std :: cout<< T不是int。 << std :: endl;
}
};

int main()
{
Point< int> intPoint;
intPoint.MyFunction();

Point< float> floatPoint;
floatPoint.MyFunction();
}

我认为是使用第一个MyFunction如果T是int,使用第二个MyFunction如果T不是int,但我得到编译器错误说错误:没有类型命名为'类型'在'struct std :: enable_if'任何人都可以指出我在这里做错了吗?

解决方案

enable_if 起作用,因为替换模板参数导致错误,因此从重载解析集中删除替换,并且编译器仅考虑其他可行的重载。 / p>

在你的例子中,在实例化成员函数时没有发生替换,因为模板参数 T 最简单的方法是创建一个默认为 T 的虚拟模板参数,并使用它来执行SFINAE。

  template< typename T> 
struct Point
{
template< typename U = T>
typename std :: enable_if< std :: is_same< U,int> :: value> :: type
MyFunction()
{
std :: cout< T是int。 << std :: endl;
}

template< typename U = T>
typename std :: enable_if< std :: is_same< U,float> :: value> :: type
MyFunction()
{
std :: cout< T不是int。 << std :: endl;
}
};






编辑 br />



正如HostileFork在注释中提到的,原始示例留给用户明确指定成员函数的模板参数并获得不正确结果的可能性。

 模板< typename T> 
struct Point
{
template< typename ... Dummy,typename U = T>
typename std :: enable_if< std :: is_same< U,int> :: value> :: type
MyFunction()
{
static_assert(sizeof ... )== 0,不要指定模板参数!
std :: cout<< T是int。 << std :: endl;
}

template< typename ... Dummy,typename U = T>
typename std :: enable_if< std :: is_same< U,float> :: value> :: type
MyFunction()
{
static_assert(sizeof ... )== 0,不要指定模板参数!
std :: cout<< T不是int。 << std :: endl;
}
};


I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this:

#include <iostream>
#include <type_traits>

template<typename T>
struct Point
{
  void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0)
  {
    std::cout << "T is int." << std::endl;
  }

  void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0)
  {
    std::cout << "T is not int." << std::endl;
  }
};

int main()
{
  Point<int> intPoint;
  intPoint.MyFunction();

  Point<float> floatPoint;
  floatPoint.MyFunction();
}

which I thought is saying "use the first MyFunction if T is int, and use the second MyFunction if T is not int, but I get compiler errors saying "error: no type named ‘type’ in ‘struct std::enable_if’". Can anyone point out what I am doing wrong here?

解决方案

enable_if works because the substitution of a template argument resulted in an error, and so that substitution is dropped from the overload resolution set and only other viable overloads are considered by the compiler.

In your example, there is no substitution occurring when instantiating the member functions because the template argument T is already known at that time. The simplest way to achieve what you're attempting is to create a dummy template argument that is defaulted to T and use that to perform SFINAE.

template<typename T>
struct Point
{
  template<typename U = T>
  typename std::enable_if<std::is_same<U, int>::value>::type
    MyFunction()
  {
    std::cout << "T is int." << std::endl;
  }

  template<typename U = T>
  typename std::enable_if<std::is_same<U, float>::value>::type
    MyFunction()
  {
    std::cout << "T is not int." << std::endl;
  }
};


Edit:

As HostileFork mentions in the comments, the original example leaves the possibility of the user explicitly specifying template arguments for the member functions and getting an incorrect result. The following should prevent explicit specializations of the member functions from compiling.

template<typename T>
struct Point
{
  template<typename... Dummy, typename U = T>
  typename std::enable_if<std::is_same<U, int>::value>::type
    MyFunction()
  {
    static_assert(sizeof...(Dummy)==0, "Do not specify template arguments!");
    std::cout << "T is int." << std::endl;
  }

  template<typename... Dummy, typename U = T>
  typename std::enable_if<std::is_same<U, float>::value>::type
    MyFunction()
  {
    static_assert(sizeof...(Dummy)==0, "Do not specify template arguments!");
    std::cout << "T is not int." << std::endl;
  }
};

这篇关于使用不同的enable_if条件选择成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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