__LINE__ as template argument (VC++)

I

Imre

Is there a Visual C++ newsgroup? I guess this question should go there,
but I couldn't find it.

Please take a look at the following little program:

template <int Line>
struct Test
{
enum { value = Line };
};

int main(int argc, char *argv[])
{
int i = Test<__LINE__>::value;
return 0;
}

Visual C++ 7.1 refuses to compile it, giving this error message for the
line in main where Test<__LINE__> is referenced:

error C2975: 'Line' : invalid template argument for 'Test',
compile-time evaluatable constant expression expected

I can't really understand what the problem is. By the time the compiler
ever sees this code, __LINE__ should already have been replaced with an
integer constant, which is definitely a compile-time evaluatable
constant expression. And in fact, if I manually replace __LINE__ with,
say, 42, it compiles fine. What's even funnier is that if I tell the
compiler to generate a preprocessed file, it compiles well.

Does anyone know what the problem is? And more importantly, what can I
do to avoid it?

Imre
 
C

Chris Jefferson

Imre said:
Is there a Visual C++ newsgroup? I guess this question should go there,
but I couldn't find it.
<snip code>
Visual C++ 7.1 refuses to compile it, giving this error message for the
line in main where Test<__LINE__> is referenced:

error C2975: 'Line' : invalid template argument for 'Test',
compile-time evaluatable constant expression expected

First of all, the standard spiel. The VC++ newsgroups should be in
microsoft.* somewhere. __LINE__ is (I'm almost certain, although I don't
have a copy of the standard to hand) a non-standard extension. Having
said that, this clearly looks like a case of VC++ playing silly buggers.

Just playing in g++ for a bit, the following code seems to work:

const int i = __LINE__;
cout << Test<i>::value;

Which might help you get around your problem.
(I'm fairly certain this is standard, once again, I'm not positive. I
should check)

Chris
 
T

Thomas Maier-Komor

Chris said:
First of all, the standard spiel. The VC++ newsgroups should be in
microsoft.* somewhere. __LINE__ is (I'm almost certain, although I don't
have a copy of the standard to hand) a non-standard extension. Having
said that, this clearly looks like a case of VC++ playing silly buggers.

__LINE__ is defined in the standard (section 16.8).

the code works fine using sun's studio compiler
and I cannot see any problem with it...

Tom
 
M

Martin Stettner

Imre said:
template <int Line>
struct Test
{
enum { value = Line };
};

int main(int argc, char *argv[])
{
int i = Test<__LINE__>::value;
return 0;
}

Visual C++ 7.1 refuses to compile it, giving this error message for the
line in main where Test<__LINE__> is referenced:

Imre
Sorry for being late ...

My VC-compiler doesn't complain about the above code (VS 2003, shows up
as Version 7.1.3088 in the about dialog...)
 
P

Peter Gordon

Imre said:
template <int Line>
struct Test
{
enum { value = Line };
};

int main(int argc, char *argv[])
{
int i = Test<__LINE__>::value;
return 0;
}

Visual C++ 7.1 refuses to compile it, giving this error message for the
line in main where Test<__LINE__> is referenced:

Imre
If the error is C4541, do the following:

1. in your project, choose Project/Settings.
2. Go to the C++ Tab
3. Change the drop-down to the C++ Language.
4. Click Enable Runtime Type Information (RTTI)
5. Rebuild your entire project.

This is from "Teach yourself C++ in 21 Days"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top