我一直在为这个问题奋斗了大约半天,至少XCode 4.6有一个bug模板类的某些声明会违反语言并允许将类内的常量数据传递给外部函数,其参数delcared as const修饰符。XCODE 4.6使用函数指针作为模板类中的参数的C++模板函数
下面的例子将编译, Tcaller ::()方法调用的甚至强硬模板声明规定作为参考 但静态CMP功能是提供无用* const的&改良剂通过常量参数。
template< typename T> struct Tcalled
{
// !!!error - this prototype doesn't protect the data passed to function
// because it should be declared with const modifiers but it wouldn't compile then.
// SEE: Below my NOTE for correct function prototype.
static bool cmp(const Tcalled*& item, const int& key) //<- correct but doesn't work
static bool cmp(Tcalled* const & item, const int& key) //<- invalid but works!!
{
return (item->index = key); /// error - we modify const object here !
}
T index;
};
template < typename T> struct Tcaller
{
Tcaller(){}
template < typename K, bool (*compare)(const T& item, const K& key) >
bool call(int k) const { return compare(data, k); }
T data;
};
int main(int argc, char *argv[])
{
const Tcaller<Tcalled<int>* > tmp; // <- const data
int k = 1;
tmp.call<int,Tcalled<int>::cmp>(k); //call here WILL modify const data !!
}
而且这样的问题:我怎么能强迫的XCode服从规则,允许我为原型 我的静态函数,因为它是申报模板参数?至于现在这些都在Xcode的错误,我是说我正确宣告我的静态方法:
呼叫到“呼叫” 候选模板不匹配的成员函数忽略:模板参数“比较”
无效显式指定的参数谢谢!
'常量Tcalled *'是指针为const Tcalled,'Tcalled * const'是常量指针Tcalled。他们是不同的东西 – yngccc 2013-04-20 22:04:08