带有模板化参数函数的模板化成员变量上的错误 C2244,仅发生在 Visual Studio 上 [英] error C2244 on templated member variable with templated argument function, only happens on Visual Studio

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

问题描述

我在 Visual Studio 上遇到了一个有趣但非常烦人的错误,下面是最简单的重现:(取消注释 #define 将允许 VS 构建代码)

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 的类之外.bar 和 bar1 在其他方面完全相同.在 VS 2010 和 VS 2012 上测试,都失败并报错:

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 中,以保持代码库干净.

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()
{
}

也有效,但它通过重新定义相同的模板参数使代码看起来很愚蠢,并且更容易出现错误.这也让界面变得凌乱.

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天全站免登陆