20
此代碼編譯失敗對於g ++(Ubuntu的/ Linaro的4.6.3-1ubuntu5)4.6.3,與此錯誤預期基本表達式而不是在Microsoft編譯
test.cpp: In function ‘T mul(V&, V&)’:
test.cpp:38:27: error: expected primary-expression before ‘>’ token
test.cpp:38:29: error: expected primary-expression before ‘)’ token
test.cpp:38:53: error: expected primary-expression before ‘>’ token
test.cpp:38:55: error: expected primary-expression before ‘)’ token
但它編譯並在Microsoft C/C++優化編譯器版本15.00.21022.08正確執行鍼對x64
#include <iostream>
#include <complex>
template <class T>
class SM
{
public:
T value;
};
template <class T>
class SC : public SM<T>
{
};
class PSSM {
public:
template <class T>
T & getSC() { return sc; }
private:
SC<double> sc;
};
class USSM {
public:
template <class T>
T & getSC() { return sc; }
private:
SC<std::complex<double> > sc;
};
template <class T, class V>
T mul(V & G, V & S) {
return (G.getSC<SC<T> >().value * S.getSC<SC<T> >().value); // error is here
}
int main() {
PSSM p;
PSSM q;
p.getSC<SC<double> >().value = 5;
q.getSC<SC<double> >().value = 3;
std::cout << mul<double>(p,q);
}
我不明白問題出在哪裏。任何人都可以理解如何解決它,或在g ++中解釋問題的性質?
但爲什麼是不是正確地分析以開始?模棱兩可出現在哪裏? – 2013-03-22 14:13:13
如果沒有'template'關鍵字,那麼一些編譯器會將類型參數列表與小於運算符混淆。 – bstamour 2013-03-22 14:15:20
最醜。消歧。永遠。 – mfontanini 2013-03-22 14:18:42