jeudi 10 février 2022

Function declaration conditioned on a used library version?

I have the following problem: say I'm using library Foo and there's a function bar that I use in my personal library. However, in an upcoming release, the function definition of bar is going to change. For example,

bar(int first, int second, int third)

will become

bar(int first, int newSecond, int second, int third)

It would be beneficial to maintain backwards compatibility in my case so I'm wondering if it's possible to define my functions conditioned on the library version

My original code:

int myFoo(int first, int second, int third){
    auto something = bar(first, second, third);
    ....
}

and the new function might be something like

int myFoo(int first, int second, int third){
    auto newSecond = X;
    auto something = bar(first, newSecond, second, third);
    ....
}

In python I could do something like

int myFoo(int first, int second, int third){
    if Foo.version == "old":
        auto something = bar(first, second, third);
    else:
        auto newSecond = X;
        auto something = bar(first, newSecond, second, third);
    ....
}

but how might I accomplish the same in C++? Would using an #IF ... work? I Googled around but the results weren't particularly useful as I don't really know the right terms to look up (happy to learn some new terms)

Aucun commentaire:

Enregistrer un commentaire