SFINAE检查operator + = [英] SFINAE check for operator+=

查看:120
本文介绍了SFINAE检查operator + =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果运算符+ = 缺失,我试图从重载集中消除重载。



知道如何检查 T + T 是否合法:

  typename T,
typename CheckTplusT = decltype(std :: declval< T>()+ std :: declval< T>())>
void foo(T a,T b,...)
{
a = a + b;
}

但不适用于 + = / code>

 模板< typename T,
typename CheckTplusT = decltype(std :: declval< T> ;()+ = std :: declval< T>())>
void foo(T a,T b,...)
{
a + = b;这是可以通过使用中的另一个表达式来修复的。
}


或者我需要另一个SFINAE构造?



我需要这个从重载集中消除的原因是它与另一个重载接受函子用作 + = 的替代。编译器是VS2013,gcc4.8

解决方案

我写第二个形式为:

  template< typename T> 
auto foo(T a,T b,...) - > decltype(a + = b,void())
{
a + = b;
}


decltype的推导类型(a + = b,如果表达式 a + = b 有效,则void()void 将只是 void



好吧,即使在第一种形式中,我也会使用尾随返回类型方法。


I'm trying to eliminate an overload from an overload set if operator+= is missing.

I know how to check if T+T is legal :

template<typename T,
         typename CheckTplusT = decltype(std::declval<T>() + std::declval<T>())>
void foo(T a, T b, ...)
{
  a = a + b;
}

but this doesn't work for +=

template<typename T,
         typename CheckTplusT = decltype(std::declval<T>() += std::declval<T>())>
void foo(T a, T b, ...)
{
  a += b;
}

Is this fixable by using another expression inside decltype or do I need another SFINAE construct?

The reason I need this eliminated from the overload set is that it clashes with another overload that accepts a functor to be used as an alternative to +=. Compilers are VS2013, gcc4.8

解决方案

I would write the second form as:

template<typename T>
auto foo(T a, T b, ...) -> decltype( a+=b, void() )
{
  a += b;
}

The deduced type for decltype(a+=b, void()) would be just void if the expression a+=b is valid, else it would result in SFINAE.

Well, even in the first form, I would use the trailing-return type approach.

这篇关于SFINAE检查operator + =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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