Overriding std Functions

I would like to override the behavior of the std function, for example std :: time. Is it possible to call std :: time and execute it through my custom function?

+4
source share
5 answers

The std does not work at all. Adding new functions, overloads, classes, or anything else to the std is ** undefined behavior *.

The only exception is specialized templates. You can provide specializations of functions in the std . The function in which this is often done is std::swap .

+10
source

That sounds like a very bad idea. A kind of redefinition of true or false . A better way would be to write your own wrapper function, for example tim_time() , which may or may not call std::time() internally.

+5
source

Not portable. With the understanding that the standard does not define what is happening, you can usually define any symbols and functions that you like in the std namespace, or a link to the library that defines these symbols, or something else. This is just undefined behavior. So all you can do is suck it and look, and hope that it does not break in the next version of your compiler.

However, most compilers are likely to work mostly if you avoid colliding with one definition with the "real" std :: time. This is due to the fact that most compilers actually do nothing special with the std namespace, and the header files and libraries that they use to implement it do not really differ from the header and library files that you could write yourself.

Dima is absolutely right, however, that it is non-standard, it is almost always a very bad idea. Maybe if you are stuck in some kind of debugging hell where you basically want to add logging to std :: time, but you can't, then you should think about it. Otherwise, do not go there. If you want to test any code to check whether it works at different times, then pass this code to the parameter (or template parameter) that determines which time function it should call.

+1
source

On some platforms you can take this off. In the source code, define a function, i.e.

 extern "C" time_t time(time_t *value) { ... } 

If you're lucky, the linker will bind your version of std::time more tightly than to the standard library. Of course, there is no guarantee that this will work or be connected the way you want. And as a flaw, you no longer have access to the original std::time .

Like everyone else, this is not portable behavior. I am sure it works on Linux. I am also sure that it does not work on Windows (the linker complains about a conflict).

+1
source

Instead of calling it std::time , route all calls that you can sometimes override through another namespace.

 namespace mystd{ using namespace std; void time() { ... } } // ... mystd::time(); // not std::time mystd::copy(...); // calls std::copy, unless you override it like time() 

Then calling mystd::time will call the modified version of the function. If you call an undefined function, for example, mystd::copy , it will be correctly resolved to the original std function.

0
source

All Articles