A bug in preprocessor (?) 
Author Message
 A bug in preprocessor (?)

I've come across the following problem.  I have a managed C++ project which
I use for migrating my existing C++ code to .Net so I can use it later from
C#.  One of the project settings specifies to use Unicode character set.
Now I have the following line in my code:

mediaDir = System::IO::Directory::GetCurrentDirectory();

And this should give me the current directory.  However, when I compile my
project I get this error:

error C2039: 'GetCurrentDirectoryW' : is not a member of
'System::IO::Directory'

Strangely enough it appears that the preprocessor completely ingored the
scope operators and treated the GetCurrentDirectory as the global macro that
then should be substituted for GetCurrentDirectoryW for unicode.

Am I missing something or is it a flat out bug?

Thank you,

Andrew



Sun, 18 Sep 2005 14:14:38 GMT  
 A bug in preprocessor (?)


Quote:
> I've come across the following problem.  I have a managed C++ project which
> I use for migrating my existing C++ code to .Net so I can use it later from
> C#.  One of the project settings specifies to use Unicode character set.
> Now I have the following line in my code:

> mediaDir = System::IO::Directory::GetCurrentDirectory();

> And this should give me the current directory.  However, when I compile my
> project I get this error:

> error C2039: 'GetCurrentDirectoryW' : is not a member of
> 'System::IO::Directory'

> Strangely enough it appears that the preprocessor completely ingored the
> scope operators and treated the GetCurrentDirectory as the global macro that
> then should be substituted for GetCurrentDirectoryW for unicode.

> Am I missing something or is it a flat out bug?

What you are missing is that the preprocessor
is not supposed to have any cognizance of
scope, or any other C or C++ semantics other
than argument list structure.  It is not a bug.

Just #undef GetCurrentDirectory or change your
#include lines to avoid that problematic #define.

--
-Larry Brasfield
(address munged, s/sn/h/ to reply)



Sun, 18 Sep 2005 20:32:22 GMT  
 A bug in preprocessor (?)



Quote:


> > I've come across the following problem.  I have a managed C++ project
which
> > I use for migrating my existing C++ code to .Net so I can use it later
from
> > C#.  One of the project settings specifies to use Unicode character set.
> > Now I have the following line in my code:

> > mediaDir = System::IO::Directory::GetCurrentDirectory();

> > And this should give me the current directory.  However, when I compile
my
> > project I get this error:

> > error C2039: 'GetCurrentDirectoryW' : is not a member of
> > 'System::IO::Directory'

> > Strangely enough it appears that the preprocessor completely ingored the
> > scope operators and treated the GetCurrentDirectory as the global macro
that
> > then should be substituted for GetCurrentDirectoryW for unicode.

> > Am I missing something or is it a flat out bug?

> What you are missing is that the preprocessor
> is not supposed to have any cognizance of
> scope, or any other C or C++ semantics other
> than argument list structure.  It is not a bug.

No, it is a Curse. :-)

Quote:
> Just #undef GetCurrentDirectory or change your

Unless you actually need that function somewhere. Or will, some day. Or are
going to change some #includes somewhere in your project, ever.

Quote:
> #include lines to avoid that problematic #define.

Don't include windows.h ? :-)

What you should do to avoid this, is to learn the entire Windows API by
heart and then never use any of these words in your own code. If you do
(when you do!), this will happen to you again, like it does to the rest of
us!

Quote:
> --
> -Larry Brasfield
> (address munged, s/sn/h/ to reply)

Using defines in this way is one of the worst design decisions made, EVER!

The entire Windows API are now reserved words for all programs that (even
indirectly) include any MS supplied header. A clear winner of the Hall of
Shame Liftime Achivement Award!

Bo Persson



Mon, 19 Sep 2005 02:21:05 GMT  
 A bug in preprocessor (?)


Quote:

...
> > What you are missing is that the preprocessor
> > is not supposed to have any cognizance of
> > scope, or any other C or C++ semantics other
> > than argument list structure.  It is not a bug.

> No, it is a Curse. :-)

I, too, dislike the indiscriminate scope
of the preprocessor.  But having learned
C long ago, I take it as a given.

Quote:
> > Just #undef GetCurrentDirectory or change your

> Unless you actually need that function somewhere. Or will, some day. Or are
> going to change some #includes somewhere in your project, ever.

One can always write GetCurrentDirectoryW
or GetCurrentDirectoryA, as appropriate.
I think some people would even prefer this!

Quote:
> > #include lines to avoid that problematic #define.

> Don't include windows.h ? :-)

It is possible to move many #include lines
well down into the code.  And windows.h
can be trimmed quite a bit by #define'ing
various preprocessor variables.

Quote:
> What you should do to avoid this, is to learn the entire Windows API by
> heart and then never use any of these words in your own code. If you do
> (when you do!), this will happen to you again, like it does to the rest of
> us!

Another approach is to confine interop
code to small stretches of code so as
to minimine this kind of problem.

...

Quote:
> Using defines in this way is one of the worst design decisions made, EVER!

And I thought it was overuse of 'static'!

Quote:
> The entire Windows API are now reserved words for all programs that (even
> indirectly) include any MS supplied header. A clear winner of the Hall of
> Shame Liftime Achivement Award!

It would be easy to write a Perl script
that would create #undef's for each and
every ANSI/Unicode #define in the headers.

If the problem really perturbs you so, a
solution is there, waiting.

--
-Larry Brasfield
(address munged, s/sn/h/ to reply)



Mon, 19 Sep 2005 05:13:42 GMT  
 A bug in preprocessor (?)

Quote:



> > I've come across the following problem.  I have a managed C++ project which
> > I use for migrating my existing C++ code to .Net so I can use it later from
> > C#.  One of the project settings specifies to use Unicode character set.
> > Now I have the following line in my code:

> > mediaDir = System::IO::Directory::GetCurrentDirectory();

> > And this should give me the current directory.  However, when I compile my
> > project I get this error:

> > error C2039: 'GetCurrentDirectoryW' : is not a member of
> > 'System::IO::Directory'

> > Strangely enough it appears that the preprocessor completely ingored the
> > scope operators and treated the GetCurrentDirectory as the global macro that
> > then should be substituted for GetCurrentDirectoryW for unicode.

> > Am I missing something or is it a flat out bug?

> What you are missing is that the preprocessor
> is not supposed to have any cognizance of
> scope, or any other C or C++ semantics other
> than argument list structure.  It is not a bug.

> Just #undef GetCurrentDirectory or change your
> #include lines to avoid that problematic #define.

In Everett, you can even do this (not sure if it was introduced in
VC7 or not):

#pragma push_macro("GetCurrentDirectory")
#undef GetCurrentDirectory

// Do whatever it is you want to do with GetCurrentDirectory

#pragma pop_macro("GetCurrentDirectory")
// GetCurrentDirectory is back to being whatever it was before, and
// you don't need to know exactly what that was.

Pretty cool, I think, though nonportable and possibly going to be
somewhat different from whatever they do to address the issue in
C++ 0x.

--
Craig Powers
MVP - Visual C++



Mon, 19 Sep 2005 06:39:31 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Preprocessor bug

2. VAX C Preprocessor bugs & workaround

3. Turbo C Preprocessor bug

4. Preprocessor x Compiler bug

5. VC Preprocessor Bug

6. BUG BUG BUG watch variable

7. Bugs Bugs Bugs galore

8. BUG BUG BUG / VIRUS VIRUS VIRUS

9. my bug or VC7 bug ?

10. Debugger bug now official MS bug

11. Attention-BUG: random() nonrandomness bug

12. please help: code bug or compiler bug?

 

 
Powered by phpBB® Forum Software