错误 C2244 在具有模板化参数函数的模板化成员变量上,仅在 Visual Studio 上发生
error C2244 on templated member variable with templated argument function, only happens on Visual Studio
我在Visual Studio上遇到了一个有趣但非常烦人的错误,下面是最简单的重现:(取消注释 #define 将允许VS构建代码)
#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 上进行测试,均失败并显示错误:
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。
我想将所有内容保存在 cpp 文件中,而不是在 .h 中以保持代码库干净。
Setting var1 to:
{
template<typename TT, TT foo>
void bar1();
}
template<typename T>
template<typename TT, TT foo>
void CC::Foo<T>::bar1()
{
}
也可以,但它通过重新定义相同的模板参数使代码看起来很愚蠢,并且更容易出现错误。这也使界面变得混乱。
通过随机输入找到修复程序,看看它是否可以编译,哈哈!!看起来有点愚蠢......
#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>();
}
我需要创建一个抽象类并使用模板参数实例化它;
想知道为什么VC不能像GCC那样自动做到这一点。
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- C++在变量给定的指定时间内关闭电脑
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 如何使布尔变量仅在设置为 true 时才为真?
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 仅在函数中设置静态变量一次
- 未定义的行为错误:对成员变量的更改仅在某些上下文中可见
- C vs 2017设置全局变量 - 程序仅在调试中使用断点
- 从C++调用 Python 或 Lua 来计算表达式,仅在需要时计算未知变量
- 具有仅在某些情况下引用外部变量的属性的类
- 如何在变量中仅允许数字
- 如果仅在 lambda 中使用,则不会在发布版本中初始化局部静态变量
- 如何仅从输入文件中"20071406"的数字中选择"2007"的数字并将其存储在变量中?
- 仅在C 中的一个变量上,两个变量函数的数值集成(使用数值配方库)
- 仅在变量命中特定值时激活的断点
- 仅在标头中的外部变量意外工作,原因是什么
- 仅在C++中传递已更改的变量
- g++-5.1.1仅在使用优化标志时警告未使用的变量
- 变量是否可以仅在 if 语句的作用域中定义,类似于通常为 for 循环所做的那样?
- 错误 C2244 在具有模板化参数函数的模板化成员变量上,仅在 Visual Studio 上发生