SFINAE 编译器问题

我的以下代码应该检测 T 是否有 beginend 方法:

The following code of mine should detect whether T has begin and end methods:

template <typename T>
struct is_container
{
    template <typename U, typename U::const_iterator (U::*)() const,
                          typename U::const_iterator (U::*)() const>
    struct sfinae {};

    template <typename U> static char test(sfinae<U, &U::begin, &U::end>*);
    template <typename U> static long test(...);

    enum { value = (1 == sizeof test<T>(0)) };
};

这是一些测试代码:

#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map>

int main()
{
    std::cout << is_container<std::vector<std::string> >::value << ' ';
    std::cout << is_container<std::list<std::string> >::value << ' ';
    std::cout << is_container<std::set<std::string> >::value << ' ';
    std::cout << is_container<std::map<std::string, std::string> >::value << '
';
}

在 g++ 4.5.1 上,输出为 1 1 1 1.然而,在 Visual Studio 2008 上,输出为 1 1 0 0.我做错了什么,或者这只是一个 VS 2008 错误?任何人都可以在不同的编译器上进行测试吗?谢谢!

On g++ 4.5.1, the output is 1 1 1 1. On Visual Studio 2008, however, the output is 1 1 0 0. Did I do something wrong, or is this simply a VS 2008 bug? Can anyone test on a different compiler? Thanks!

推荐答案

Stephan T. Lavavej 有 这个要说:

Stephan T. Lavavej has this to say:

请注意,技术上禁止获取标准库成员函数的地址.(它们可能被重载,使 &foo::bar 模棱两可,他们可以有额外的默认参数,挫败通过 static_cast 消除歧义的尝试.)

Please note that it is technically forbidden to take the address of a Standard Library member function. (They can be overloaded, making &foo::bar ambiguous, and they can have additional default arguments, defeating attempts to disambiguate via static_cast.)

所以我想我会使用更简单的版本,它只检查嵌套的 const_iterator 类型.

So I guess I'm going to use the simpler version that only checks for the nested const_iterator type.

相关文章