std::tuple get() member function

后端 未结 3 1313
故里飘歌
故里飘歌 2020-12-04 01:17

boost::tuple has a get() member function used like this:

tuple t(5, \"foo\", \"bar\");
cout << t.g         


        
相关标签:
3条回答
  • 2020-12-04 01:48

    The existing answers are great and certainly for the standards committee were vital for this purpose. But there is another issue that I think is important enough to mention.

    With free functions, you have the ability to modify an interface without changing the definition of a class. You can make any type "gettable" simply by specializing the global get. With a member function, you would have to directly modify the class.

    Range-based for looks for member begin/end on class types, but it also looks for non-member begin/end via ADL. These APIs can be used with any container, even those that don't have begin/end functions. You can specialize it for, for example, LibXML2 element types, such that you can range-based for over xmlElement*'s.

    You can't do that if they had to be member functions.

    In C++, free functions are a natural interface for many operations that could be done on many different kinds of classes.

    0 讨论(0)
  • 2020-12-04 02:01

    From C++0x draft:

    [ Note: The reason get is a nonmember function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword. — end note ]

    This can be illustrated with this code:

    template <typename T>
    struct test
    {
      T value;
      template <int ignored>
      T&  member_get ()
      {  return value;  }
    };
    
    template <int ignored, typename T>
    T&  free_get (test <T>& x)
    {  return x.value;  }
    
    template <typename T>
    void
    bar ()
    {
      test <T>  x;
      x.template member_get <0> ();  // template is required here
      free_get <0> (x);
    };
    
    0 讨论(0)
  • 2020-12-04 02:11

    N3090/3092, §20.4.2.6/8: "Note: The reason get is a nonmember function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword. —end note"

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