dimanche 1 mars 2015

Overload operator const char* for retrofitting in-place?

I'm working with a bunch of existing tests like the following (there are literally a couple of hundred of them):



bool Test_X(const char* file) {
...
}


And it would be called like:



result = Test_X("TestData/test-set-X.bin");


The problem is after make install, the TestData is no longer in $PWD so the test program fails with a file not found error.


I want to create a Locate class that tries to abstract away the differences. To make it minimally invasive, I want to use it like:



result = Test_X(Locate("TestData/test-set-X.bin"));


To make that work, I think need to overload operator const char* (and some friends). However, that runs afoul with the rule:



  • Automatic casts are almost always evil


See, for example, this question/answer on Stack Overflow: Why does std::string not provide a conversion to const char*?.


The problem gets a little more complex because the tests need to work when faced with make PREFIX=/usr/local && sudo make install PREFIX=/usr/local. So I need to propagate the make directory variables down into the C++ code.


Is the overload my only option and therefore a necessary evil in this case? Or are there other solutions I am missing?


Aucun commentaire:

Enregistrer un commentaire