I stumbled on the same problem of nested templates described here.
The following code :
# include <cstdlib> // for std::size_t
template<std::size_t N>
class Outer
{
public :
template<typename T>
class Inner
{
public :
inline Inner<T> & operator ++ (void) ;
inline Inner<T> operator ++ (int) ;
} ;
} ;
template<std::size_t N>
template<typename T>
inline typename Outer<N>::template Inner<T> & Outer<N>::Inner<T>::operator ++ (void)
{ // ^^^^^^^^ Point Of Interest : MSVC is the only one who complains
// preincrement
}
template<std::size_t N>
template<typename T>
inline typename Outer<N>::template Inner<T> Outer<N>::Inner<T>::operator ++ (int)
{ // ^^^^^^^^ Point Of Interest
// postincrement
}
只要返回型号有<代码>template的关键词,与MinGW 4.5(即gcc)一起汇编罚款,但MSVC2010对申报/定义不匹配提出抱怨:
error C2244: Outer::Inner::operator ++ : unable to match function definition to an existing declaration 1> definition 1> Outer::Inner &Outer::Inner::operator ++(void) 1> existing declarations 1> Outer::Inner Outer::Inner::operator ++(int) 1> Outer::Inner &Outer::Inner::operator ++(void) <- This is what it wants !
在删除<代码>template关键词时,MSVC汇编了罚款,而gcc成为愤怒:
error: non-template Inner used as template note: use Outer::template Inner to indicate that it is a template
How to adapt the above code in order to compile with both MSVC and GCC ? I really want to avoid this awful preprocessor hack :
# ifdef MSVC # define nested_template # else # define nested_template template # endif
然后,使用<条码>,在MSVC, 即<条码>。
- If impossible, how to refactor/redesign the code to avoid the nested templates case ?