Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ bind function

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use C++ bind function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the C++ bind function.

1. The origin of use

We see that the code I use is boost:: bind, and most of what you see on the Internet is the standard function std:: bind. In fact, boost:: bind is a generalization of the standard functions std:: bind1st and std:: bind2nd. It supports arbitrary function objects, function pointers and member function pointers, and can bind any parameter to a specific value or route input parameters to any location. Bind has no requirements for function objects; in particular, it does not require result_type,first_argument_type and the second_argument_type standard typedef.

two。 Introduction to use

Usually we can think of the bind function as a general-purpose function adapter, which accepts a callable object and generates a new callable object to "adapt" the argument list of the original object. Bind can construct a new callable object based on the existing callable object. With bind, we can realize the function of "dynamically generating new function". In short, we can modify the original function and generate an object that can be called through the bind function, similar to the overloading of the function, but we don't need to rewrite a function, we can do it with the bind function.

Let's take a look at how the bind function is used.

Bind a normal function and a function pointer:

Int fun (int a, int bjingint ccent int djint e) {return a + b-c + d-e;} int main () {int Xue1 for Y2 and auto g = bind (fun,x,y,_2,z,_1);}

Such g is a callable object with two parameters represented by placeholders _ 2 and _ 1, respectively. The new callable object binds its own parameters to the given values x, y, and z as the third and fifth arguments passed to the fun,fun function.

Bind a member function:

One of the most commonly used functions of bind is to construct bind objects from class member functions; think about it, how to construct callback functions from class member functions (non-static member functions)? The answer is that it is difficult, but it can be easily done through bind.

Class Speaker {public Speaker (); ~ Speaker () {speaker_play_routine_- > join ();} void playRoutine () {} private boost::thread* speaker_play_routine_ {nullptr};} int main () {speaker_play_routine_ = new boost::thread (boost::bind (& Speaker::playRoutine, this));}

Here, the member function of a class must be called through an object or pointer of the class, so when binding, bind takes out the location of the first parameter to specify an instance, pointer, or reference of a class.

Note: the address operator & must be added before the member function.

At this point, I believe you have a deeper understanding of "how to use C++ bind function". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report