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
For what it's worth, this is suppose to be valid code. __LINE__
is suppose to behave as if it were:
#define __LINE__ 0
Of course, replacing 0 with the current line number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With