7

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?

Bob
  • 805
  • 1
  • 6
  • 6
  • What version? Works in VS2010. (I have done this before, but tried your code anyway.) – GManNickG Mar 22 '11 at 21:03
  • @GMan: Interesting, tested too in VS2010 and it doesn't work for me. o_O (Or do you mean with the `static const int` defined in global scope? Which would actually defeat the purpose...) – Xeo Mar 22 '11 at 21:06
  • @GMan: I'm running a clean install of 2010. – Bob Mar 22 '11 at 21:06
  • @Bob: @Xeo: Wtf...What version? Mine's: "10.0.30319.1 RTMRel". – GManNickG Mar 22 '11 at 21:07
  • @Xeo: That's very wrong, then. Do you guys have the service pack installed? (I don't.) – GManNickG Mar 22 '11 at 21:09
  • @GMan: Not that I know of. Of course VS could've installed it without me realizing, but I don't think so. – Xeo Mar 22 '11 at 21:11
  • @Xeo: Hm. Can you run the Visual Studio Command Line and run "cl"? My reports version: "16.00.30319.01". – GManNickG Mar 22 '11 at 21:13
  • @GMan: I have the same version of visual studio and the compiler as you. I don't have the service pack installed. – Bob Mar 22 '11 at 21:14
  • 1
    @Bob: If you are enabling `/ZI` (edit and continue debugging) option, does disabling this solve the problem? – Ise Wisteria Mar 22 '11 at 21:14
  • @Bob @Xeo: Try this: Go to your Project Property Pages, expand the C/C++ folder, go to General and set "Suppress Startup Banner" to "No". This will cause the compiler to show its version number to the Output window when you compile. That way you get the version number of the compiler Visual Studio is actually using. – In silico Mar 22 '11 at 21:15
  • @Bob: Is your `#define` for `__LINE__` somehow different from ours? Right click on `__LINE__` and select "go to declaration". – John Dibling Mar 22 '11 at 21:18
  • @John: That's built into the language (compiler), you can't see its declaration. – GManNickG Mar 22 '11 at 21:19
  • @Ise, @Bob: Well I don't get that one bit. – John Dibling Mar 22 '11 at 21:19

3 Answers3

13

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

MacGucky
  • 2,494
  • 17
  • 17
2

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
0

@Bob, you are going to love this one!

I was interested in your question so I tried your code. It compiled in g++ but fails with your error in MSVC10. To investigate, I used Google to find out how to see the preprocessor output: you set "Properties | C++ | Preprocessor | Preprocess to a file" to true. And then I compiled again... AND IT WORKED! Turns out if this option is disabled, the compile fails; if it's enabled, the compile works. I suppose MS doesn't bother to generate LINE entries unless the preprocessor output is being captured. Oy va voy!

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186