0
class C
{
    struct S
    {
        T a;
        T2 b;
        .
        .
        .
        T z;
    };

    int compute(S s[]);
}

So I need this compute() method to work on the structure S in on of two ways (runtime selectable).

  • One case is to estimate something on base of a, b and the other contents of structure S, excluding z.

  • Other times I need the exact same computations, but taking z instead (and in place of) a. They both are the same type and have the same meaning.

The structure S is exposed in the API and thus need to be stored in exactly this layout.

What would be an efficient (compute() is being called rather often) end elegant solution? bool parameter? enum parameter? Template parameter (if so, how to implement it)?


NOTES:

  • compute() is quite a long function, with selecting a or z happening exactly once
Vorac
  • 8,726
  • 11
  • 58
  • 101

2 Answers2

1

I'd use an enum. It's no more or less efficient than bool, but it may be more clear at the call site:

compute(s, UseAB);
compute(s, UseZB);

Instead of:

compute(s, false);
compute(s, true);

The template option is possible but probably not better; you should try the regular run-time way first. If you care a lot about performance, consider making compute() inline if it is short and simple.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'd definitely go for enums over bools in this case, since there's no clear relationship between truth value and field choice. If performance is equal (or not different by any relevant amount), choose the more readable option. You will thank yourself later. – Matthew Walton Oct 07 '14 at 08:58
0

I would just use bool parameter if these are the only two cases. Keep it simple. And document it properly.

Or you can have two methods computeA(S s) and computeZ(S s). In this case it is really about what you are comfortable with.

zegkljan
  • 8,051
  • 5
  • 34
  • 49
  • 1
    I would definitely vote for the two method solution, because OP stated that the method would be called quite often and the overhead of passing +1 parameter can be avoided this way. If the methods are even inline then no extra space is required in the binary compared to the one method case. – Géza Török Oct 07 '14 at 10:18
  • @GézaTörök However, in the NOTES section of the question it is stated that the ``compute()`` method is quite long with the decision happening once. Should two methods be used, they would either have to be almost identical or they would have to make the decision and then call some other method doing the actual work. And I doubt that passing an extra parameter is more expensive than calling another function. And a question is whether this optimization is really so big that it has some real impact. But I'm not that much of an expert. – zegkljan Oct 07 '14 at 10:24
  • I was hoping for something like method template, indicating which is *the* field - something, that is possible in python, but maybe not in C++. The function is quite coherent and I wouldn't want to split it into 3 parts. Accepting, because I went with the bool - simplest. – Vorac Oct 07 '14 at 13:18