lundi 13 janvier 2020

Simplifying C++ pattern that includes variable name and function output

I have this pattern too many times in my code:

class c_outcome {
    public:
        bool success;
        std::string error;
};

c_outcome out;
out = do_sth(a, input, "a");
if (!out.success) { return do_sth2(out.error); };
out = do_sth(b, input, "b");
if (!out.success) { return do_sth2(out.error); };
out = do_sth(c, input, "c");
if (!out.success) { return do_sth2(out.error); };

How can I shorten it avoiding repetitions? Basically, I would want to:

  • Simplify the pattern do_sth(a, input, "a"). Maybe following: Alternatives to stringifying the variable name in C++11

  • Integrate in one function: out = do_sth(a, input, "a"); if (!out.success) { return do_sth2(out.error); };

  • Combine everything in one function like: do_everything(input, a, b, c)

I don't know if this is possible/convenient, but it doesn't look right to repeat it continuously.

Aucun commentaire:

Enregistrer un commentaire