English 中文(简体)
MSVC2010年 Nest版模板
原标题:Nested templates workaround for MSVC2010?

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

加入,MSVC似乎有过错。 因此,我的问题是:

  • 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 ?
最佳回答

实际上,定义是在你们需要加固模板的许多地方交叉汇编者支持的途径。 Boo!

另一种选择是,在额外负责人中保存所有你的封顶模板,并在特别志愿人员委员会和海合会有不同的途径。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签