removing namespace std

K

K.I.

Sorry if this is not the right place to post.

I am replacing all strcpy with my own customized strcpy, hence I did
the following in a shared header file :
#define strcpy STRCPY
inline STRCPY(...)
{
....
}

somewhere in another part of the program called std::strcpy(), and
when I compiled it, it complained STRCPY is not member of std. Besides
changing std::strcpy, is there another way to allow std::strcpy to be
replaced, or to be rejected?

Thank you.
 
D

dizzy

K.I. said:
Sorry if this is not the right place to post.

I am replacing all strcpy with my own customized strcpy, hence I did
the following in a shared header file :
#define strcpy STRCPY
inline STRCPY(...)
{
...
}

Good luck with that on implementations that have internal (as oposed to
normal external library function) strcpy() or implementations that have
strcpy() a macro itself (usually depending on other macros).

I think it's better to just change your code from calling strcpy to call
your own function, myns::strcpy(). Oh, and "don't use any strcpy(), they
are are unsafe"(tm).
 
M

Michael DOUBEZ

K.I. a écrit :
Sorry if this is not the right place to post.

I am replacing all strcpy with my own customized strcpy, hence I did
the following in a shared header file :
#define strcpy STRCPY
inline STRCPY(...)
{
...
}

An easier way would be to do:
namespace mystd {
inline char* strcpy(...)
{
//...
}
}
using mystd::strcpy;

But it doesn't translate calls to std::strcpy().
somewhere in another part of the program called std::strcpy(), and
when I compiled it, it complained STRCPY is not member of std. Besides
changing std::strcpy, is there another way to allow std::strcpy to be
replaced, or to be rejected?

Since you already seems ready to use unsafe, hideous and forbidden
methods, here is what you can do:

//define your function
namespace mystd {
inline char* strcpy(...)
{
//...
}
}


//create an alias in std
//this is forbidden by the standard
namespace std {
namespace mystd {
static inline char* strcpy(...)
{
return ::mystd::strcpy(...);
}
}}

// replace strcpy with strcpy in your namespace
#define strcpy mystd::strcpy

Then the define will make the following:
strcpy() -> mystd::strcpy()
::strcpy() -> ::mystd::strcpy()
std::strcpy() -> std::mystd::strcpy()
 
K

K.I.

Thank you for the solution, I am aware of the risk taken, the
implementation is just a temporary measure to detect a certain point
of failure, though it may not be the best way out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top