这是我检查类是否具有成员函数的代码begin
:
template<typename T> struct has_begin
{
struct dummy {typedef void const_iterator;};
typedef typename std::conditional< has_iterator<T>::yes, T, dummy>::type TType;
typedef typename TType::const_iterator Iter;
struct fallBack{ Iter begin() const ; Iter end() const;};
struct checker : T, fallBack {};
template <typename B, B> struct cht;
template<typename C> static char check(cht< Iter (fallBack::*)() const, &C::begin>*); // problem is here
template<typename C> static char (&check(...))[2];
public:
enum {no = (sizeof(check<checker>(0))==sizeof(char)),
yes=!no};
};
如果我将cht
in的第二个参数更改check(cht< Iter (fallBack::*)() const, &C::begin>*);
为
&checker::begin
,这不会改变代码的语义,因为cht
的第二个模板参数总是checker
由于这个enum {no = (sizeof(check<checker>(0))==sizeof(char))
但是代码更改现在会导致错误:
prog.cpp: In instantiation of 'has_begin<std::vector<int> >':
prog.cpp:31:51: instantiated from here
prog.cpp:23:38: error: reference to 'has_begin<std::vector<int> >::checker::begin' is ambiguous
我想知道这种行为背后的原因是什么。