Hi,
I need to make a stub for some standard library function, but the compiler gets confused about which function I want to call in my code. I thought that putting the stubbed function together with its caller into an annonymous namespace will give higher precedence to the stub, but it didn't.
Here is an example code:
main.cpp: In function ‘void {anonymous}::test()’:
main.cpp:23:47: error: call of overloaded ‘sigaction(int, sigaction*, NULL)’ is ambiguous
main.cpp:23:47: note: candidates are:
main.cpp:8:5: note: int {anonymous}::sigaction(int, const sigaction*, sigaction*)
In file included from main.cpp:4:0:
/usr/include/signal.h:266:12: note: int sigaction(int, const sigaction*, sigaction*)
Could anyone tell me is it possible to force the compiler to use my stubbed version of sigaction() and if yes, how to do that? I am not allowed to modify test() function.
The obvious, brute-force approach is to change how you link the program, so that it doesn't link against the library that contains the definition of the function you're trying to stub out.
In TDD, this is known as a "link seam". However, that's harder to do with standard library functions than it would be with a function you'd written, because you may need other functions from that library. You'd have to stub out every function you're using from that library. That could be a lot of work, or not much work, depending on how many functions you're using.
If you were allowed to modify test(), then my answer would be to pass in a callback function pointer, and maybe have that default to the standard lib sigaction().
But wouldn't that then involve having to modify test() ?
No. Both sigaction() and test() reside in the same namespace. An anonymous namespace and the global namespace are accessed the same way hence it's ambiguous. You have to access test() differently. Like so: