Why is the use of tuples in C++ not more common?

前端 未结 12 517
-上瘾入骨i
-上瘾入骨i 2021-01-29 23:48

Why does nobody seem to use tuples in C++, either the Boost Tuple Library or the standard library for TR1? I have read a lot of C++ code, and very rarely do I see the use of tup

相关标签:
12条回答
  • 2021-01-30 00:15

    As many people pointed out, tuples are just not that useful as other features.

    1. The swapping and rotating gimmicks are just gimmicks. They are utterly confusing to those who have not seen them before, and since it is pretty much everyone, these gimmicks are just poor software engineering practice.

    2. Returning multiple values using tuples is much less self-documenting then the alternatives -- returning named types or using named references. Without this self-documenting, it is easy to confuse the order of the returned values, if they are mutually convertible, and not be any wiser.

    0 讨论(0)
  • 2021-01-30 00:16

    I have a feeling that many use Boost.Any and Boost.Variant (with some engineering) instead of Boost.Tuple.

    0 讨论(0)
  • 2021-01-30 00:17

    But what if you want to rotate three values?

    swap(a,b);
    swap(b,c);  // I knew those permutation theory lectures would come in handy.
    

    OK, so with 4 etc values, eventually the n-tuple becomes less code than n-1 swaps. And with default swap this does 6 assignments instead of the 4 you'd have if you implemented a three-cycle template yourself, although I'd hope the compiler would solve that for simple types.

    You can come up with scenarios where swaps are unwieldy or inappropriate, for example:

    tie(a,b,c) = make_tuple(b*c,a*c,a*b);
    

    is a bit awkward to unpack.

    Point is, though, there are known ways of dealing with the most common situations that tuples are good for, and hence no great urgency to take up tuples. If nothing else, I'm not confident that:

    tie(a,b,c) = make_tuple(b,c,a);
    

    doesn't do 6 copies, making it utterly unsuitable for some types (collections being the most obvious). Feel free to persuade me that tuples are a good idea for "large" types, by saying this ain't so :-)

    For returning multiple values, tuples are perfect if the values are of incompatible types, but some folks don't like them if it's possible for the caller to get them in the wrong order. Some folks don't like multiple return values at all, and don't want to encourage their use by making them easier. Some folks just prefer named structures for in and out parameters, and probably couldn't be persuaded with a baseball bat to use tuples. No accounting for taste.

    0 讨论(0)
  • 2021-01-30 00:20

    You rarely see them because well-designed code usually doesn't need them- there are not to many cases in the wild where using an anonymous struct is superior to using a named one. Since all a tuple really represents is an anonymous struct, most coders in most situations just go with the real thing.

    Say we have a function "f" where a tuple return might make sense. As a general rule, such functions are usually complicated enough that they can fail.

    If "f" CAN fail, you need a status return- after all, you don't want callers to have to inspect every parameter to detect failure. "f" probably fits into the pattern:

    struct ReturnInts ( int y,z; }
    bool f(int x, ReturnInts& vals);
    
    int x = 0;
    ReturnInts vals;
    if(!f(x, vals)) {
        ..report error..
        ..error handling/return...
    }
    

    That isn't pretty, but look at how ugly the alternative is. Note that I still need a status value, but the code is no more readable and not shorter. It is probably slower too, since I incur the cost of 1 copy with the tuple.

    std::tuple<int, int, bool> f(int x);
    int x = 0;
    std::tuple<int, int, bool> result = f(x); // or "auto result = f(x)"
    if(!result.get<2>()) {
        ... report error, error handling ...
    }
    

    Another, significant downside is hidden in here- with "ReturnInts" I can add alter "f"'s return by modifying "ReturnInts" WITHOUT ALTERING "f"'s INTERFACE. The tuple solution does not offer that critical feature, which makes it the inferior answer for any library code.

    0 讨论(0)
  • 2021-01-30 00:21

    The C++ tuple syntax can be quite a bit more verbose than most people would like.

    Consider:

    typedef boost::tuple<MyClass1,MyClass2,MyClass3> MyTuple;
    

    So if you want to make extensive use of tuples you either get tuple typedefs everywhere or you get annoyingly long type names everywhere. I like tuples. I use them when necessary. But it's usually limited to a couple of situations, like an N-element index or when using multimaps to tie the range iterator pairs. And it's usually in a very limited scope.

    It's all very ugly and hacky looking when compared to something like Haskell or Python. When C++0x gets here and we get the 'auto' keyword tuples will begin to look a lot more attractive.

    The usefulness of tuples is inversely proportional to the number of keystrokes required to declare, pack, and unpack them.

    0 讨论(0)
  • 2021-01-30 00:29

    Not everyone can use boost, and TR1 isn't widely available yet.

    0 讨论(0)
提交回复
热议问题