【发布时间】:2011-04-21 18:15:06
【问题描述】:
template<typename U> struct CheckSignature {
enum {SizeTrue = 1, SizeFalse = 2};
typedef char ReturnTrue[SizeTrue];
typedef char ReturnFalse[SizeFalse];
typedef typename U::iterator (U::*InsertSig)(typename U::iterator, typename const U::value_type &);
static ReturnTrue &CheckInsert(InsertSig);
static ReturnFalse &CheckInsert(...);
static const bool value = (sizeof(CheckInsert(&U::insert)) == sizeof(ReturnTrue));
};
int main() {
CheckSignature<std::string >::value; //compile error
CheckSignature<std::vector<int> >::value; // OK
return 0;
}
此代码为字符串类生成了一个编译错误,指出 2 个重载都不能转换所有参数类型。但是,对于矢量,它编译得很好。当参数不是 InsertSig 类型时,重载解析不应该选择 CheckInsert(...) 吗?
【问题讨论】:
-
你得到什么编译错误?
-
@Tim error C2665: 'CheckSignature::CheckInsert' : 2 个重载都不能转换所有参数类型
-
你的
InsertSig是什么? -
下面是指向成员函数的指针:
typedef typename U::iterator (U::*InsertSig)(typename U::iterator, typename const U::value_type &);
标签: c++ templates visual-c++ stl