我剛剛遇到編譯失敗時,從VS2013一些代碼移植到GGC 4.9和Clang 3.5(使用libC++)。代碼的要點是SFINAE std :: isfinite和類似的函數使用std :: is_arithmetic
#include <cmath>
struct Foo
{
operator double() const { return(101.0); } // Implicit conversion to double
};
int main(int, char**)
{
Foo foo;
std::exp(foo); // Compiles
std::isfinite(foo); // Does not
return(0);
}
我相信isfinite
調用不會編譯,因爲isfinite
funtion在CMATH有返回類型聲明爲:
typename std::enable_if<std::is_arithmetic<_A1>::value, bool>::type
因爲Foo
不is_arithmetic
,isfinite
被刪除從超載設置。 isfinite
的朋友也是如此,例如isnan
。所以我的問題是這是否是預期的。
標準是否要求像isfinite
這樣的函數的參數實際上是直接double
或float
而不是被隱式轉換爲它們?
另外我有點不確定爲什麼是std::is_arithmetic
不是std::is_floating_point
,不是is_arithmetic
暗示isfinite
整數?
作爲一個額外的問題什麼是指定約束的最佳方式,如is_convertible_to_floating_point?
由於C++ 11 ['std :: exp'](http://en.cppreference.com/w/cpp/numeric/math/exp)也接受整數類型,所以不僅是浮點類型。 – 2014-12-06 21:13:47
爲了解決您的問題,您可以輕鬆地爲您的課程添加['std :: is_arithmetic'](http://en.cppreference.com/w/cpp/types/is_arithmetic)專業化。 – 2014-12-06 21:14:54
@JoachimPileborg不,你不能。專門的標準庫類型特徵(除了'std :: common_type')是UB。 – 2014-12-06 21:20:33