问题描述
我希望以下代码可以正常工作,但我收到了编译错误:
#include< iostream>
using namespace std;
template< int N>
struct foo
{
foo(){cout< N< endl }
};
int main()
{
foo< __LINE__> F;
}
为什么会发生这种情况?在模板实例化之前,我通过 __ LINE __
粘贴行号?
如果我想这样做只是引入 static const int
来保存行号或有标准解决方案?
适用于VS 2010 10.0.40219.1 SP1Rel和
中的 但是MSDN提到问题,这会导致C2975,如果在编译器选项 / ZI中使用
: p> __ LINE __
编辑:对不起,我链接了德国版本
I expected the following code to work, but I received a compile error:
#include <iostream>
using namespace std;
template<int N>
struct foo
{
foo() { cout << N << endl; }
};
int main()
{
foo< __LINE__ > f;
}
Why does this happen? I though __LINE__
would paste in the line number before template instantiation occurred?
If I wanted to do this should I just introduce a static const int
to hold the line number or is there a standard solution?
Works for me in VS 2010 10.0.40219.1 SP1Rel and in Ideone
But MSDN mentions problems which result in C2975, if using __LINE__
in template with compiler-option /ZI
: MSDN C2975
Edit: Sorry, I linked the german version, here in english
这篇关于在visual studio中使用__LINE__宏作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!