在visual studio中使用__LINE__宏作为模板参数 [英] Using __LINE__ macro as a template parameter in visual studio

查看:221
本文介绍了在visual studio中使用__LINE__宏作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


 错误C2975:'n':'foo'无效的模板参数,期望的编译时常数表达式




  #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和 Ideone

中的

但是MSDN提到问题,这会导致C2975,如果在编译器选项 / ZI中使用 __ LINE __ MSDN C2975 p>

编辑:对不起,我链接了德国版本这里以英语


I expected the following code to work, but I received a compile error:

error C2975: 'n' : invalid template argument for 'foo', expected compile-time constant expression

#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__宏作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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