By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,411 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,411 IT Pros & Developers. It's quick & easy.

__LINE__ as template argument (VC++)

P: n/a

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

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Imre wrote:
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
Jul 23 '05 #2

P: n/a
Chris Jefferson wrote:

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

--
__________________________________________________ ______________________
Dipl.-Ing. Thomas Maier-Komor http://www.rcs.ei.tum.de
Institute for Real-Time Computer Systems (RCS) fon +49-89-289-23578
Technische Universitaet Muenchen, D-80290 Muenchen fax +49-89-289-23555
Jul 23 '05 #3

P: n/a
Imre wrote:

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...)

--
Martin

Jul 23 '05 #4

P: n/a
Martin Stettner <no****@martin.dot.stettner.at.complement.dot.at > wrote
in news:cv*********@newsreader1.utanet.at:
Imre wrote:

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"
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.