在Visual Studio中使用__LINE__宏作为模板参数

Bob*_*Bob 7 c++ visual-studio

我希望以下代码可以工作,但我收到了一个编译错误:

error C2975: 'n' : invalid template argument for 'foo', expected compile-time constant expression
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;

template<int N>
struct foo
{
    foo() { cout << N << endl; }
};

int main()
{
    foo< __LINE__ > f;
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我__LINE__会在模板实例化之前粘贴行号吗?

如果我想这样做,我应该引入一个static const int来保存行号或是否有标准解决方案?

Mac*_*cky 13

适用于VS 2010 10.0.40219.1 SP1Rel和Ideone

但MSDN提到了导致C2975的问题,如果__LINE__在带有编译器选项的模板中使用/ZI:MSDN C2975

编辑:对不起,我挂了德语版,英语在这里