此代码块通过SFINAE检测std::vector<T>::push_back
方法,但在4.9系列之前的gcc中,不适用于 const 向量,你可以看看是否尝试this online compiler。它仅在4.9系列之后起作用。我试着在release notes找到解决这个问题的方法,但我无法找到它。哪些变化解决了它?
#include <vector>
template <typename Type, typename Return, typename P1, Return (Type::*Pointer)(P1)> struct S1PMemberMethod { };
template <typename T> struct SMemberMethodChecker
{
template <typename Type> static char HasReferencePushBackMethod(S1PMemberMethod<Type, void, const typename Type::value_type &, &Type::push_back> *);
template <typename Type> static long HasReferencePushBackMethod(...);
template <typename Type> static char HasPushBackMethod(S1PMemberMethod<Type, void, typename Type::value_type, &Type::push_back> *);
template <typename Type> static long HasPushBackMethod(...);
static const bool HAS_PUSH_BACK_METHOD = ((sizeof(HasReferencePushBackMethod<T>(0)) == sizeof(char)) || (sizeof(HasPushBackMethod<T>(0)) == sizeof(char)));
};
int main()
{
// Compiler error when push_back is not detected
int A[ SMemberMethodChecker< const std::vector<int> >::HAS_PUSH_BACK_METHOD - 1 ];
}