473,802 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__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 2377
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.u tanet.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
3920
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
597
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
1455
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 list AbstractDataHolderList* // Used to delete this object and the item it points to AbstractDataHolderList::DeleteThisItem(void) {
17
2021
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 in advance of instantiating Command, it works. When I construct the T class *in* the Command constructor, it doesn't work. Any ideas? ================ CODE FOLLOWS =============== #include <iostream>
11
3101
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 there a workaround? template <typename T> class Base { }; template <typename U, template <typename> class TempTemp> class Derived;
5
579
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 I found that I had to use a kludgy way to stick in the line number. Thanks, Song ///// Code Snippet ///// #include <stdio.h>
1
3983
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 arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
3
1415
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 template argument? Please note that I cannot use the ‘new’ operator ( as long as it allocates memory ), see the following code snnipet as an example of what I am trying to do.
2
6651
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
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10303
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10061
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9111
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.