我粘貼了我正在處理的複雜模板代碼片段的最小值。成員模板外部類的函數定義無法構建Visual Studio 2013
1 template <typename T>
2 class Base
3 : public T
4 {
5 public:
6 template <typename W, W& (T::ToImpl::*Func)()>
7 bool Foo();
8 };
9
10 template <typename T>
11 template <typename W, W& (T::ToImpl::*Func)()>
12 bool Base<T>::Foo()
13 {}
14
15 int
16 main()
17 {
18 return 0;
19 }
該代碼是非常簡單的,所以我不會解釋任何東西。我無法用Visual Studio 2013(又名VC++ 12)編譯此代碼。它給出了以下錯誤:
main.cc(13): error C2244: 'Base<T>::Foo' : unable to match function definition to an existing declaration
definition
'bool Base<T>::Foo(W)'
existing declarations
'bool Base<T>::Foo(W)'
出於好奇我試着用g ++(4.4.7)編譯上面的代碼,它編譯得很好。
我將不勝感激,如果有人可以提供解釋爲什麼代碼無法在Windows上編譯?修復會更加甜美。 :)
什麼是'ToImpl'? – Axalo 2015-02-11 00:36:03
@Axalo它將是模板類中定義的類型。類似這樣的 - class base { public: typedef SomeClass ToImpl; } –
slowstart
2015-02-11 00:43:04