简体   繁体   English

为什么boost建议使用核心功能而不是成员函数?

[英]Why does boost recommend using core functions over member functions?

In the documentation for boost.geometry it states boost.geometry的文档中,它说明了

Note: prefer using x = bg::get :<0>(point1); 注意:更喜欢使用x = bg :: get :<0>(point1);
(as opposed to x = point1.get<0>();) (而不是x = point1.get <0>();)

I have seen this elsewhere in the boost docs. 我在boost docs的其他地方看过这个。 My question is why? 我的问题是为什么? Is this a best-practices thing, a performance thing or some quirk? 这是最好的做法,表演还是一些怪癖? Is it a general rule or specific to this library? 这是一般规则还是特定于此库?

It's not boost per se, but modern C++ API design. 它本身并不是提升,而是现代C ++ API设计。

  • By not requiring member functions, you can adapt your own classes and even third party library types to work with the boost Api of your choice. 通过不要求成员函数,您可以调整自己的类甚至第三方库类型以使用您选择的boost Api。 (This way you can eg make types from a third party library serializable to a Boost Serialization archive). (这样您就可以将类型从第三方库序列化为Boost Serialization存档)。

  • Also, by making the functions free functions, there is an improved decoupling of dependencies. 此外,通过使函数自由函数,可以改善依赖关系的解耦。 Eg: fusion/tuple.hpp doesn't need to depend on anything IO related, because the streaming operations are free functions, and hence can be declared (and defined) in a separate header: fusion/tuple_io.hpp . 例如: fusion/tuple.hpp不需要依赖任何与IO相关的东西,因为流操作是自由函数,因此可以在单独的头中声明(和定义): fusion/tuple_io.hpp

  • It also helps encapsulation because by default the free functions aren't friend s of the host class (and as such are unable to access private members). 它还有助于封装,因为默认情况下,自由函数不是宿主类的friend (因此无法访问私有成员)。

  • free functions can "Do The Right Thing" based on ADL: 免费功能可以基于ADL“做正确的事”:

     using std::swap; swap(a, b); // will lookup `swap` in the namespaces that declare the parameter types 

    (several other namespaces are also used for lookup) (其他几个名称空间也用于查找)

  • Finally, free functions can generically service a group of types, that need not be OO-related (inheritance related). 最后,自由函数可以一般地为一组类型提供服务,这些类型不需要与OO相关(与继承相关)。 In this way, free functions encourage avoiding duplication of code. 通过这种方式,自由函数可以避免重复代码。

Edit Addressing the question of why you should prefer the non-member syntax, if both exist : 编辑解决为什么您应该更喜欢非成员语法的问题, 如果两者都存在

  • it works for types that don't have the member function 它适用于没有成员函数的类型
  • it doesn't require .template disambiguation in template code (as pointed out by @Simple) 它不需要模板代码中的.template消歧(正如@Simple所指出的那样)
  • Again: it's not boost specific. 再说一遍:这不是特定的提升。

    • c++03 has had std::swap() as a free function c ++ 03将std::swap()作为自由函数
    • c++11 introduces std::begin() and std::end() as free functions c ++ 11将std::begin()std::end()引入为自由函数
    • std::hash<> , std::less<> , std::greater<> , std::equal_to<> similarly provide customization points that are not intrusive (but aren't functions of course) std::hash<>std::less<>std::greater<>std::equal_to<>同样提供非侵入性的定制点 (当然不是函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM