473,396 Members | 1,982 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,396 software developers and data experts.

namespace and #define

Hello

I noticed that when I use #define inside a namespace then this #define only
works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X

--
Elias
Jul 22 '05 #1
10 19205

"lallous" <la*****@lgwm.org> wrote in message
Hello

I noticed that when I use #define inside a namespace then this #define only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};
There is no semicolon after a namespace definition.

Now can be used as:

test::X


IMO, this is not correct behavior. Macros do replacement without taking
account namespace rules etc. So macro should expand test::X to test::int
which is non-sensical. Just using X should have been good.

Sharad

Jul 22 '05 #2
So macro should expand test::X to test::int


Replace macro with preprocessor.
Jul 22 '05 #3
"lallous" <la*****@lgwm.org> wrote in message
news:2u*************@uni-berlin.de...
I noticed that when I use #define inside a namespace then this #define
only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific? Definitely not standard.
And I think you misinterpreted what happened.
I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X


No compiler should accept this, and I'm not familiar
with any such extension in VC7 or elsewhere.
Could you post the exact code that lead you to your conclusion?

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #4
"lallous" <la*****@lgwm.org> wrote in message
news:2u*************@uni-berlin.de...
I noticed that when I use #define inside a namespace then this #define only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?


Compiler specific. #define is a pre-processor thing, and knows nothing
about namespaces. Maybe they have a switch to turn off this behavior?
Check on a microsoft newsgroup (several on msdn.microsoft.com).

Jul 22 '05 #5
lallous posted:
Hello

I noticed that when I use #define inside a namespace then this #define
only works when prefixed with the namespace name.

Really? Please name the compiler, I want to give it a standing ovation.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X

It can also be used as simply:
X
which is one reason why you can't do:

namespace Win
{
#include <windows.h>
}
-JKop
Jul 22 '05 #6
> Hello

I noticed that when I use #define inside a namespace then this #define
only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X


Thank you all for your replies.

For those who replied:
Now can be used as:

test::X

It can also be used as simply: X


That's what I suspected w/o trying the code, but after testing it, it didn't
work.
I had to specify which namespace that #define belongs to.

The code I ran into isn't mine and seeing a #define inside a namespace made
me pose this question to the newsgroup.

For those who have VC7, please try this code:

#include <string>

namespace std
{
#ifdef UNICODE
//typedef string stringT;
#define stringT wstring
#else
//typedef wstring stringT;
#define stringT string
#endif
}

int main()
{
// works:
std::stringT test;

// doesn't work (unless you state the namespace) (error C2065)
stringT test1;

return 0;
}

I believe adding "typedefs" as I commented out is a better solution.

--
Elias
Jul 22 '05 #7

"lallous" <la*****@lgwm.org> wrote in message
news:2u*************@uni-berlin.de...
Hello

I noticed that when I use #define inside a namespace then this #define
only works when prefixed with the namespace name.

Is this behaviour standard or just compiler specific?

I use VC7.

namespace test
{
#define X int
};

Now can be used as:

test::X

Thank you all for your replies.

For those who replied:
Now can be used as:

test::X

It can also be used as simply: X


That's what I suspected w/o trying the code, but after testing it, it

didn't work.
I had to specify which namespace that #define belongs to.

The code I ran into isn't mine and seeing a #define inside a namespace made me pose this question to the newsgroup.

For those who have VC7, please try this code:

#include <string>

namespace std
{
#ifdef UNICODE
//typedef string stringT;
#define stringT wstring
#else
//typedef wstring stringT;
#define stringT string
#endif
}

int main()
{
// works:
std::stringT test;

// doesn't work (unless you state the namespace) (error C2065)
stringT test1;

return 0;
}


Of course it doesn't work. This is completely standard behaviour. You are
misunderstanding what is happening. It is because the macro is working that
you get an error, not because it isn't. Macros pay no attention to
namespaces at all.

In the code that doesn't work the macro stringT expands to string, so the
line of code looks like this

string test1;

In the code that does work the macro stringT expands to string, so the line
of code looks like this

std::string test;

The case that doesn't work is an error because string is not qualified with
a namespace, macros have nothing to do with it. In /both/ cases the macro
worked and expanded to exactly the same result.

Undoubtedly typedefs are the correct way to do it, but adding typedefs to
the std namespace is not legal, even if VC accepts it. Here is how you
should do it

#include <string>

#ifdef UNICODE
typedef std::string stringT;
#else
typedef std::wstring stringT;
#endif

or you could even do it like this (VC++ specific code)

#include <string>
#include <tchar.h>

typedef std::basic_string<_TCHAR> stringT;

john
Jul 22 '05 #8
Hello John,
#include <string>

namespace std
{
#ifdef UNICODE
//typedef string stringT;
#define stringT wstring
#else
//typedef wstring stringT;
#define stringT string
#endif
}

int main()
{
// works:
std::stringT test;

// doesn't work (unless you state the namespace) (error C2065)
stringT test1;

return 0;
}
Of course it doesn't work. This is completely standard behaviour. You are
misunderstanding what is happening. It is because the macro is working
that
you get an error, not because it isn't. Macros pay no attention to
namespaces at all.


Yes, my fault, I didn't see that till you pointed it out.

Undoubtedly typedefs are the correct way to do it, but adding typedefs to
the std namespace is not legal.
Why isn't it legal?

Syntax wise or coding standards wise?
even if VC accepts it. Here is how you
should do it

#include <string>

#ifdef UNICODE
typedef std::string stringT;
#else
typedef std::wstring stringT;
#endif


Now that's a typedef solution, but you put "std::" before it? (A)

I tried to define as:
int main()
{
stringT test; // <-- it works
std::stringT test1; // <-- it doesn't since stringT isn't a part of std
namespace; check (A)
}

--
Elias
Jul 22 '05 #9
Please discard my questions...I must haven't got enough sleep yesterday.

--
Elias
Jul 22 '05 #10
> >
Undoubtedly typedefs are the correct way to do it, but adding typedefs to the std namespace is not legal.


Why isn't it legal?


Short answer, because the standard say so.

Long answer, I would guess, is to give implementors the latitude to
implement the std namespace in a way that wouldn't work if you tried to add
new names to it.

The only thing you are allowed to do is specialise templates that already
exist in the standard namespace. So this would be legal

class SpecialChar
{
...
};

namespace std
{
template <class Tr, class Al>
class string<::SpecialChar, Tr, Al>
{
...
};
}

Here you are defining your own version of std::string that uses SpecialChar.
But adding new names is a no-no. This does cause problems sometimes.

john
Jul 22 '05 #11

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

Similar topics

2
by: Anonymous | last post by:
How do I reference a namespace variable in a multi-file project? Do I use the keyword 'extern'? If so, does the word 'extern' modify the namespace or the individual variables within the...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some...
3
by: John Ratliff | last post by:
I have a program, the classes that belong to it all belong to a special namespace I created for them. This had lead to a minor issue I don't quite understand. For some reason, when I return a...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
4
by: rmacias | last post by:
I posted this in another forum, but nobody replied and I need an answer fairly quickly: I'm creating a Windows form application in VC++ .NET 2003. I'm creating the UI is a nested namespace as...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
1
by: Ray | last post by:
Hello, I'm reading Mr. Flanagan's JS Definitive Guide 5th edition. I was wondering about a point he make in section 10.2 of the book: "Importing Symbols from Namespaces". He mentions in there...
9
by: vthomasset | last post by:
Hi, Sorry for the bad subject, but i couldn't figure a better one. I would like to understand why a variable declared non static in a header causes multiple symbol definitions (if included in...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
0
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...
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...

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.