LoginSignup
0
0

More than 3 years have passed since last update.

Boost BindとPlaceholderに関する雑感

Last updated at Posted at 2020-08-15

個人的な経験から、初めてTensorflowを扱ったとき、placeholderが把握できなかった経験があった。
従来のC/C++の変数とは少し違うためである。

しかし、BoostのBind関数から見るとわかった。

 1#include <iostream>
 2#include <boost/bind.hpp>
 3using namespace std;
 4using namespace boost;
 5
 6void func_for_bind( const char* s1, const char* s2,
 7               const char* s3, const char* s4 )
 8{
 9    cout << s1 << ' ' << s2 << ' '
10         << s3 << ' ' << s4 << endl;
11}
12
13int main()
14{
15    // placeholder: _1, _2                                                                                                                                                            
16    bind( &func_for_bind, _1, _2, "three", "four" )( "one", "two" );
17    bind( &func_for_bind, "one", _1, "three", _2 ) ( "two", "four" );
18    bind( &func_for_bind, _1, _1, " (ノД`)", _2 )( "(゚∀゚)", "(´ω`)" );
19
20    return 0;
21}

16行目でbindはプレースホルダー _1, _2に "one", "two"を渡している。
プレースホルダー_1, _2は、専門的に言うと「関数に渡される遅延引数の記述位置」


./a.out 
one two three four
one two three four
(゚∀゚) (゚∀゚)  (ノД`) (´ω`)

最近のプログラマな人は、「Pythonから入ったのでPlaceholder抵抗ありません」という人が多いのだらうか?
(*´Д`)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0