473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

__LINE__ as template argument (VC++)


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
4 2338
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
17
by: Alexander Stippler | last post by:
Hi, what do I have to do to get this (incorrect) piece of code to work. The specialization is wrong, but how can I do it? template <typename T, typename V> class Mask { public: Mask(int i)...
8
by: | last post by:
This code get's a link error (I'm guessing because the template isn't being instantiated)... // For using templates to simplify deleting items template <class T> // Returns the next ADHL in the...
17
by: mrstephengross | last post by:
I've got a 'Command' class whose constructor takes an instance of a template argument T. The constructor then prints out the name of the class T that was passed to it. When I construct the T class...
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
5
by: Generic Usenet Account | last post by:
Can someone kindly explain why stringification of the compiler preprocessor macro __LINE__ requires two steps, instead of one? I wanted to pass the error location of a system call to perror() and...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
3
by: =?Utf-8?B?TmFkYXY=?= | last post by:
Hi, I am trying to manually call a constructor of a template argument, the compiler returns “error C2039: ‘T’ : is not a member...” How can I manually call the constructor of a...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.