SFINAE从另一个命名空间测试一个自由函数

时间:2012-01-06 10:57:21

标签: c++ sfinae

我试图想出一个hack来测试std::isnan是否在预处理器中没有特殊的外壳编译器的情况下定义,并提出了以下内容,我希望它能正常工作。

#include <cmath>
#include <type_traits>

namespace detail {
    using namespace std;

    struct dummy {};
    void isnan(dummy);

    //bool isnan(float); // Just adding this declaration makes it work!

    template <typename T>
    struct is_isnan_available {
        template <typename T1>
        static decltype(isnan(T1())) test(int);
        template <typename>
        static void test(...);

        enum { value = !std::is_void<decltype(test<T>(0))>::value };
    };
}

int main() {
    return detail::is_isnan_available<float>::value;
}

结果it doesn't detect it。我知道某些std::isnan是在ideone上定义的,因为我手动测试了它。

当我uncomment the marked line above时,它有效。

我在这里缺少什么?是什么解释了这种行为?

2 个答案:

答案 0 :(得分:7)

问题是,using指令不会向当前命名空间添加成员,因此std::成员仍然可以被此命名空间中的声明隐藏。

using std::isnan的行为就像导入的命名空间的成员被添加到包含use - 位置和导入的命名空间的命名空间一样。 using声明是命名空间中的普通声明,因此可以使用后面的声明参与重载解析。

但是,正如评论中指出的那样,如果函数不存在,则会产生错误。要解决这个问题,您需要put it out of your detail:: namespace then。这应该有效,因为导入的定义与dummy重载处于同一级别。您可以将重载带到全局命名空间,也可以创建辅助命名空间(在全局命名空间中)和import both

答案 1 :(得分:1)

我解决了POSIX线程安全API集合的问题,它取代了非线程安全的标准函数:C++11 alternative to localtime_r。此代码检测是否在全局命名空间中定义了API,如果它不存在,则选择自定义解决方法。