我们来看下面的示例程序:
#include <cmath>
namespace half_float
{
template<typename T> struct half_expr {};
struct half : half_expr<half>
{
operator float() const;
};
template<typename T> half sin(const half_expr<T>&);
template<typename T> half atan2(const half_expr<T>&, const half_expr<T>&);
}
using namespace std;
using half_float::half;
int main()
{
half a, b;
half s = sin(a);
half t = atan2(a, b);
}
Run Code Online (Sandbox Code Playgroud)
在VS 2010中,这编译得很好(暂时忽略明显的链接器错误).但在VS 2012中,这给了我:
错误C2440:'转换':无法从'float'转换为'half_float :: half'
所以似乎重载解析不会从命名空间中选择版本half_float(ADL应该完成),而是std使用隐式转换来实现float.但奇怪的是,这只发生在atan2通话而非sin通话中.
在较大的项目中,这个错误实际上首先发生在我身上,它也发生在其他2参数函数(或者更确切地说是那些有2个half参数的函数)中fmod,但不适用于任何1参数函数.同样在较大的项目中它也适用于 …