Tuple and Tie

 recently came across an interesting feature to C++11: std::tie. One of the features I love about python is that you are able to return multiple values from a function. These get returned in a structure called a “Tuple”. Here is a basic example of what I mean:


class rectangle:
      def __init__(self, width, height):
          self.width = width
          self.height = height

      def get_dimensions(self):
          return self.width, self.height

  r = rectangle(3,4)
  w, h = r.get_dimensions()
  print w,h

When run this will print “3 4”. The classic way to return multiple parameters in C/C++ is to pass extra parameters by reference or as pointers. The return values then got written to these parameters. While this worked it wasn’t the clearest thing to read since there was no clear division between parameters and return values. Infact, sometimes they served the same purpose of providing data and returning with a value. Here is an example of the same function in the old C++ way:

#include <iostream>
  using std::cout;

  class rectangle
  {
    public:
      rectangle(int _width, int _height) : width(_width), height(_height) {}
      int width, height;

      void get_dimensions(int & _width, int & _height)
      {
          _width = width;
          _height = height;
      }
  };

  int main(int argc, char** argv)
  {
      rectangle r(3,4);
      int w,h;
      r.get_dimensions(w,h);
      cout << w << ' ' << h << '\n';
      return 0;
  }


As you can see  w  and  h  are passed in like normal parameters with no indication to the user their real purpose.

Luckily C++11 has introduced tuple types and a way to return multiple values from a function. To use this all you need to do is return a tuple from a function, and then use std::tie to accept the return value. std::tie create its own tuple storing references to the variables it needs to throw the data into. Heres the same example:

#include <tuple>
  #include <iostream>

  using std::tuple;
  using std::tie;
  using std::make_tuple;
  using std::cout;

  class rectangle
  {
    public:
      rectangle(int _width, int _height) : width(_width), height(_height) {}
      int width, height;

      tuple<int, int> get_dimensions() {return make_tuple(width, height);}
  };

  int main(int argc, char** argv)
  {
      rectangle r(3,4);
      int w,h;
      tie(w,h) = r.get_dimensions();
      cout << w << ' ' << h << '\n';
      return 0;
  }

As you can see, it is quite clear that  w  and  h  are  return  values and not parameters. They even get returned with a call to  return . In today’s age with our large code bases I think it is important that we make a move towards readability or we’ll never be able to maintain anything.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值