473,569 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19233

"lallous" <la*****@lgwm.o rg> 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.o rg> 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.o rg> 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.o rg> 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
misunderstandin g 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_stri ng<_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
misunderstandin g 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

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

Similar topics

2
5687
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 namespace? For example, suppose I have the following 3 files: -----------------------//myns.h #include <iostream> using namespace std;
4
7102
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
7501
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 namespace definition with class definitions. The book says "C++ has a global anonymous namespace that is similar to Java's global anonymous...
3
1316
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 reference class in this namespace from a method in a class in the same namespace, the compiler makes me fully qualify the return type. It may have...
20
3841
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 a named namespace namespace foo { class Foo
4
1531
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 follows: namespace MyApplication { namespace GUI {
11
2047
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
4281
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 that one can create an alias for the symbol defined in a namespace like this: // This is an easier name, to save typing.
9
2760
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 different compilation units) while the same declaration in a namespace does not. Also, what is the correct way to declare a scoped variable in a...
0
7703
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...
0
7618
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...
1
7678
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...
1
5514
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...
0
5222
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...
0
3656
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.