Elegant ways to return multiple values from a function

后端 未结 14 877
梦谈多话
梦谈多话 2020-12-15 03:49

It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing.

The typical soluti

14条回答
  •  暖寄归人
    2020-12-15 04:35

    Pretty much all ML-influenced functional langues (which is most of them) also have great tuple support that makes this sort of thing trivial.

    For C++ I like boost::tuple plus boost::tie (or std::tr1 if you have it)

    typedef boost::tuple XYZ;
    
    XYZ foo();
    
    double x,y,z;
    boost::tie(x,y,z) = foo();
    

    or a less contrived example

    MyMultimap::iterator lower,upper;
    boost::tie(lower,upper) = some_map.equal_range(key);
    

提交回复
热议问题