What is the reason for `std::make_tuple`?

前端 未结 4 1876
鱼传尺愫
鱼传尺愫 2021-01-03 19:21

I mean why does std::make_tuple exist? I know that there are situations where the function reduces the amount of characters you have to type because you can avo

4条回答
  •  攒了一身酷
    2021-01-03 19:55

    Because you cannot use argument deduction for constructors. You need to write explicitly std::tuple(i,d);.

    It makes it more convenient for creating a tuple and passing it to another function in one-shot.

    takes_tuple(make_tuple(i,d)) vs takes_tuple(tuple(i,d)).

    One less place to change when the type of i or d changes, especially if there were possible conversions to between the old and new types.

    If it were possible to write std::tuple(i,d);, make_* would (probably) be redundant.

    (Don't ask why here. Maybe for similar reasons why syntax A a(); does not invoke a default constructor. There are some painful c++ syntax peculiarities.)

    UPDATE NOTE: As Daniel rightly notices, c++17 will be enhanced, so that template argument deduction will work for constructors, and such delegation will become obsolete.

提交回复
热议问题