Следующий мой код должен определить, есть ли у T
методы begin
и end
:
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 << '\n';
}
В g ++ 4.5.1 вывод 1 1 1 1
. Однако в Visual Studio 2008 вывод будет 1 1 0 0
. Я что-то сделал не так, или это просто ошибка VS 2008? Кто-нибудь может протестировать на другом компиляторе? Спасибо!