473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preprocessor seems interfering namespace defines?

Hi, all:
I have a question regarding to how to solve following problem:
I have header called myHeader.h which #define MAX_LEN 100 (legacy
code).
Now, I like to put most commonly used in myHeader.h into a namespace,
e.g.,

#include <myHeader.h>
namespace myNamespace
{
const int MAX_LEN = 100;
MyData data[MAX_NUM]; //MyData defined in myHeader.h
};

Unfortunately, I have to include myHeader.h and compiler fails me due
to that MAX_LEN is probably replaced with 100 by preprocessor since it
complains about syntax error, what is best way to solve this?
thx

May 30 '07 #1
9 2337
On 5/30/2007 3:20 PM, we*****@yahoo.c om wrote:
Hi, all:
I have a question regarding to how to solve following problem:
I have header called myHeader.h which #define MAX_LEN 100 (legacy
code).
Now, I like to put most commonly used in myHeader.h into a namespace,
e.g.,

#include <myHeader.h>
#ifdef MAX_LEN
#undef MAX_LEN
#endif
namespace myNamespace
{
const int MAX_LEN = 100;
MyData data[MAX_NUM]; //MyData defined in myHeader.h
You certainly meant:
MyData data[MAX_LEN];
};

Unfortunately, I have to include myHeader.h and compiler fails me due
to that MAX_LEN is probably replaced with 100 by preprocessor since it
complains about syntax error, what is best way to solve this?
If you can, ditch the preprocessor usage in 'myHeader.h'.
Regards,
Stefan
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
May 30 '07 #2
we*****@yahoo.c om wrote:
>
Unfortunately, I have to include myHeader.h and compiler fails me due
to that MAX_LEN is probably replaced with 100 by preprocessor since it
complains about syntax error, what is best way to solve this?
thx
Use a different name for your macro.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 30 '07 #3
Pete Becker wrote:
we*****@yahoo.c om wrote:
>>
Unfortunatel y, I have to include myHeader.h and compiler fails me due
to that MAX_LEN is probably replaced with 100 by preprocessor since it
complains about syntax error, what is best way to solve this?
thx

Use a different name for your macro.
s/macro/object/.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 30 '07 #4
On 30 Maj, 15:20, "wenm...@yahoo. com" <wenm...@yahoo. comwrote:
Hi, all:
I have a question regarding to how to solve following problem:
I have header called myHeader.h which #define MAX_LEN 100 (legacy
code).
Now, I like to put most commonly used in myHeader.h into a namespace,
e.g.,

#include <myHeader.h>
namespace myNamespace
{
const int MAX_LEN = 100;
MyData data[MAX_NUM]; //MyData defined in myHeader.h

};

Unfortunately, I have to include myHeader.h and compiler fails me due
to that MAX_LEN is probably replaced with 100 by preprocessor since it
complains about syntax error, what is best way to solve this?
thx
Your problem would never have occured had you followed the good advice
to reserve names all in uppercase for macroes. Rename your constant to
max_len.

/Peter

May 30 '07 #5
Thanks all for replying to my question.

I have other problem which is not caused by our code, instead from
Linux/system headers.

I have defined a file scope var, e.g., in my.C:

#include "my.h"

const int MAX_INT = std::numeric_li mits<int>::max( );
:
:

When I compile, max is replaced with a macro defined in /usr/include/
LiS/sys/LiS/share.h
#define max(a,b) (((a)>(b))?(a): (b))

And compiler complains:
`max' is not a member of type `
std::numeric_li mits<int>'

how am I going to use soemthing like
"std::numeric_l imits<int>::max ();"?

My platform is:
Linux server 2.4.21-15.ELsmp #1 SMP Thu Apr 22 00:18:24 EDT 2004 i686
i686 i386 GNU/Linux

May 31 '07 #6

<we*****@yahoo. comwrote in message ...
>
I have other problem which is not caused by our code, instead from
Linux/system headers.

I have defined a file scope var, e.g., in my.C:

#include "my.h"
const int MAX_INT = std::numeric_li mits<int>::max( );
:
When I compile, max is replaced with a macro defined in /usr/include/
LiS/sys/LiS/share.h
#define max(a,b) (((a)>(b))?(a): (b))

And compiler complains:
`max' is not a member of type `
std::numeric_li mits<int>'
Did you:
#include <limits>
#include <limits>
#include <iostream>
#include <iomanip>
int main(){
std::cout <<" 0x"<<std::hex
<<std::numeric_ limits<int>::ma x()<<std::endl;
return 0;
}

--
Bob R
POVrookie
May 31 '07 #7
On May 31, 5:38 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
<wenm...@yahoo. comwrote in message ...
I have other problem which is not caused by our code, instead from
Linux/system headers.
I have defined a file scope var, e.g., in my.C:
#include "my.h"
const int MAX_INT = std::numeric_li mits<int>::max( );
:
When I compile, max is replaced with a macro defined in /usr/include/
LiS/sys/LiS/share.h
#define max(a,b) (((a)>(b))?(a): (b))
And compiler complains:
`max' is not a member of type `
std::numeric_li mits<int>'

Did you:
#include <limits>

#include <limits>
#include <iostream>
#include <iomanip>
int main(){
std::cout <<" 0x"<<std::hex
<<std::numeric_ limits<int>::ma x()<<std::endl;
return 0;
}

--
Bob R
POVrookie
yes, I did. For a simple test, it compiles/works fine without
including share.h. share.h is indirectly included by other legacy
code, preprocessor seems failing the includes.

Jun 1 '07 #8
we*****@yahoo.c om wrote:
>
yes, I did. For a simple test, it compiles/works fine without
including share.h. share.h is indirectly included by other legacy
code, preprocessor seems failing the includes.
Right. the problem is the macro max(), which conflicts with the member
function with the same name (the algorithm max has the same problem, but
it sounds like you haven't hit that one yet).

Under Windows, the solution is to define _NO_MINMAX (or something like
that, it's been a while) before including the offending files. If there
isn't a mechanism like that for the file that's messing up your code,
one approach would be to start every list of include files with:

#include "share.h"
#undef max

That way you'll blow away the macro before it does any damage to headers
that aren't prepared for it. On the other hand, it might cause problems
for other headers from the same package that assume that the macro
definition is still there. If that's the case, one possibility (NOT
TESTED) would be:

#include "share.h"
#undef max
#include <algorithm>
using std::max;

That kills the macro, but puts the C++ algorithm named max in the global
namespace, where subsequent headers will see it. I wouldn't do this
automatically, only if it's needed to solve actual problems. It may well
be that the macro max is only used in the library's source files, so all
you need to do is get rid of it.

Another approach, if you don't mind doing a little surgery on your
compiler's headers, is to find all their uses of max and replace them
with (max) (i.e. put parentheses around them). The preprocessor won't
treat a name as a function-like macro if it's not followed by a left
parenthesis, so it won't mess with those uses of max. You'll have to do
the same in your source code if you use max:

return (max)(a, b);

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 1 '07 #9
On Jun 1, 10:12 am, Pete Becker <p...@versatile coding.comwrote :
wenm...@yahoo.c om wrote:
yes, I did. For a simple test, it compiles/works fine without
including share.h. share.h is indirectly included by other legacy
code, preprocessor seems failing the includes.

Right. the problem is the macro max(), which conflicts with the member
function with the same name (the algorithm max has the same problem, but
it sounds like you haven't hit that one yet).

Under Windows, the solution is to define _NO_MINMAX (or something like
that, it's been a while) before including the offending files. If there
isn't a mechanism like that for the file that's messing up your code,
one approach would be to start every list of include files with:

#include "share.h"
#undef max

That way you'll blow away the macro before it does any damage to headers
that aren't prepared for it. On the other hand, it might cause problems
for other headers from the same package that assume that the macro
definition is still there. If that's the case, one possibility (NOT
TESTED) would be:

#include "share.h"
#undef max
#include <algorithm>
using std::max;

That kills the macro, but puts the C++ algorithm named max in the global
namespace, where subsequent headers will see it. I wouldn't do this
automatically, only if it's needed to solve actual problems. It may well
be that the macro max is only used in the library's source files, so all
you need to do is get rid of it.

Another approach, if you don't mind doing a little surgery on your
compiler's headers, is to find all their uses of max and replace them
with (max) (i.e. put parentheses around them). The preprocessor won't
treat a name as a function-like macro if it's not followed by a left
parenthesis, so it won't mess with those uses of max. You'll have to do
the same in your source code if you use max:

return (max)(a, b);

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Thanks Pete. I have not traced back where share.h is included in our
code, but most likely it is include indirectly by other headers. I
guess that I may end up using #undef to solve the problem.

Jun 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1701
by: Jakob Simon-Gaarde | last post by:
Some project includes files from different libraries lib1,lib2 and lib3 all having each there own version header file. I need to be able to pick up these values in a single define value DEP_PROJECT. pseudo preprocessor code would look like this: 1. Lib1 version header is included: #define PROJ_NAME "Lib1" #define PROJ_VERSION 102
2
1511
by: Pelle Beckman | last post by:
Hi, This might be OT... Are there preprocessor defines in the C++ standard, such as __FILE__, __LINE__, __NAMESPACE__, __FUNCTION__, ec? If there is, were can I find a list? If there isn't, does anyone know of a good list for those defines under Gcc (3.4 <=)?
13
2139
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int defined = 2;
9
6583
by: ccwork | last post by:
Hi all, We can define some magic number with #define: #define OPERATION_1 0x00000001 #define OPERATION_2 0x00000002 #define OPERATION_3 0x00000003 .... We can also do that with enum: enum OPERATION
3
13625
by: CPA Study Group | last post by:
Is it possible to write a C Preprocessor macro which changes the case of a string. This will save me time by not having to use awk of write my own preprocessor. Here is a sample code which I want to work --------- #ifdef LOWER #define NORMAL(x) MKLOWER(x) #else #define NORMAL(x) MKUPPER(x) #endif
5
2474
by: tb2000 | last post by:
Here's my scenario: VS2005, .NET and .NETCF C# projects mices in a single solution file, referencing the same source code base (the NETCF projects use reflinks to the NET sources) Now I am trying the following #if !NETCF {some code which is only compileable under .NET but now is excluded} #endif NETCF is defined in compiler settings
31
2940
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
5
15745
by: John Speth | last post by:
Hi Group- I want to use the C preprocessor to generate expanded text as a text processor for software test script generation. The preprocessor output will never be compiled. I need to insert newlines in the preprocessed output file but I can't figure out how to do it. Here's a stab at what I'm trying to do but failing: #define CMD(s) text s \
3
303
by: Nathan Moinvaziri | last post by:
I am wonder if there is a way to use preprocessor definitions to expose code only if a particular file is included in a project. I am targeting the msvc. I am thinking of something like #if defined(filecabinet.h) // if filecabinet.h is part of the project ....
0
9706
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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
10332
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...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
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
5522
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.