错误C2244与模板参数模板函数成员变量,只有可能发生在Visual Studio中 [英] error C2244 on templated member variable with templated argument function, only happens on Visual Studio

查看:390
本文介绍了错误C2244与模板参数模板函数成员变量,只有可能发生在Visual Studio中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的,但非常恼人的,错误在Visual Studio中,下面是最简单的摄制:(注释中的#define将允许VS打造code)

I ran into an interesting, yet extremely annoying, bug on Visual Studio, below is the simplest repro: (uncomment the #define will allow VS to build the code)

#include <iostream>
using namespace std;

//#define BUILD_ON_VS

class CC
{
public:
   template<typename T>
   struct Foo
   {
      template<T foo>
      void bar()
      {
         cout << "VC likes this!\n";
      }
#ifndef BUILD_ON_VS
      template<T foo>
      void bar1();
#endif
   };

   Foo<int> m_foo;
};

#ifndef BUILD_ON_VS
template<typename T>
template<T foo>
void CC::Foo<T>::bar1()
{
   cout << "VC doesn't like this...\n";
}
#endif

int main()
{
   CC cc;
   cc.m_foo.bar<-1>();
#ifndef BUILD_ON_VS
   cc.m_foo.bar1<2>();
#endif
   return 0;
}

基本上,我不能把功能栏定义的类以外在Visual Studio。酒吧和BAR1是完全一样的,否则。
试验于2010年和VS 2012 VS,都失败,错误:

Basically, I cannot put the definition of the function bar outside of the class in Visual Studio. bar and bar1 are exactly the same otherwise. Test on VS 2010 and VS 2012, both failed with errors:

error C2244: 'CC::Foo<T>::bar1' : unable to match function definition to an existing declaration
definition
'void CC::Foo<T>::bar1(void)'
existing declarations
'void CC::Foo<T>::bar1(void

然而,它适用于所有在线的编译器,如compileonline和ideone。

It, however, works on all online compilers, such as compileonline and ideone.

我要保留一切cpp文件里,而不是在.H保持code基清洁。

I want to keep everything inside the cpp file, not in the .h to keep the code base clean.

Setting var1 to:
{
   template<typename TT, TT foo>
   void bar1();
}

template<typename T>
template<typename TT, TT foo>
void CC::Foo<T>::bar1()
{
}

也可以,但是它使code相通过重新定义相同的模板参数白痴和更容易产生错误。这也使得界面杂乱。

Also works, but it makes the code looks idiotic by redefining the same template parameter and much more prone to bugs. It also makes the interface messy.

推荐答案

通过随机打字,看它是否编译,哈哈找到了解决!看起来有点傻...

Found a fix by typing randomly and see if it compiles, haha!!! looks kind of stupid...

#include <iostream>
using namespace std;

//#define BUILD_ON_VS

class CC
{
public:

   template<typename T>
   struct Foo;

   Foo<int>* m_foo;

   template<typename T>
   struct Foo
   {
      template<T foo>
      void bar();
   };
};

template<typename T>
template<T foo>
void CC::Foo<T>::bar()
{
   cout << "VC happen to like this...\n";
}

int main()
{
   CC cc;
   cc.m_foo = new CC::Foo<int>;
   cc.m_foo->bar<2>();
}

我需要一个抽象类和模板参数初始化它;

I need to make an abstract class and instantiate it with template arguments;

不知道为什么VC不能做到这一点像自动GCC。

Wonder why VC can't do this automatically like GCC.

这篇关于错误C2244与模板参数模板函数成员变量,只有可能发生在Visual Studio中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆