我一直在爲這個問題奮鬥了大約半天,至少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