[過去ログ] Boost総合スレ part7 (989レス)
上下前次1-新
抽出解除 レス栞
このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
471(11): デフォルトの名無しさん [sage] 2009/04/30(木) 23:11:00 AAS
C++ code - 32 lines - codepad
外部リンク:codepad.org
このコードにて、エラーになる原因が分かりません。
私の考えでは
boost::lambda::bind(func_i, boost::lambda::protect(boost::lambda::bind(my_name, boost::lambda::_1)))
でconst char* constを受け取りintを返す関数が得られるので、
それをlambda_test_funcの引数として渡せるのではないかと思ったのですが。
どこを修正すればよろしいでしょうか?
472(3): デフォルトの名無しさん [sage] 2009/04/30(木) 23:17:13 AAS
>>471
lambda で生成される関数は、関数オブジェクトの一種。
関数へのポインタは C++ 言語上の「関数」を指すことができるけど、
言語上はクラスオブジェクトとなる関数オブジェクトを指すことはできない。
関数と関数オブジェクトを同等に扱うためのものとして boost::function がある。
- void lambda_test_func(int (*func_ptr)(const char* const char_ptr))
+ void lambda_test_func(boost::function<int (const char*)> f)
473(1): 472 [sage] 2009/04/30(木) 23:38:58 AAS
>>471
あと、関数合成に boost::lambda::protect 要らない。
外部リンク[html]:www.boost.org
475: 471 [sage] 2009/05/01(金) 05:58:59 AAS
>>472
ありがとうございます。
学べました!
485: 471 [sage] 2009/05/03(日) 14:36:17 AAS
>>472-473
ご教示の通り
boost::lambda::protectを外した上で boost::function を利用したところ、
問題無くコンパイル通りました。
ありがとうございました。
487: 471 [sage] 2009/05/03(日) 17:55:45 AAS
普通の部分では以前ご教示いただけた通りで動きました。
今度はBoost.Spiritのセマンティックアクションにラムダ式を入れる時にまた困っております。
//以下はMyClass.hの中身
class MyClass
{
MyClass();
virtual ~MyClass()=0;
public:
static const int get_10(const char * const);
};
//以下はMyClass.cppの中身
#include "MyClass.h"
const int MyClass::get_10(const char * const)
{return 10;}
//mainは以下です。
C++ code - 112 lines - codepad
外部リンク:codepad.org
このソース中の質問箇所というコメントのところ
('/' >> fctr)[
boost::lambda::bind(func_geti_reti,
boost::lambda::bind(MyClass::get_10, boost::lambda::_1)
)
]
が問題点なのですが、
488: 471 [sage] 2009/05/03(日) 17:58:47 AAS
前述のように
@Boost.Spiritのセマンティックアクションに
Aラムダのbindで関数を結合し
BMyClass::get_10の部分ががクラスのstaticメンバ関数である
この@〜Bの時に
x_error.cpp:82: error: invalid initialization of non-const reference of type 'co
nst int (&)(const char*)' from a temporary of type 'const int ()(const char*)'
C:/BoostC++Libraries/boost/lambda/detail/bind_functions.hpp:256: error: in passi
ng argument 1 of `const boost::lambda::lambda_functor<boost::lambda::lambda_func
tor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambd
a::detail::unspecified> typename boost::lambda::detail::bind_tuple_mapper<typ
ename boost::lambda::detail::constify_non_funcs<T>::type, const Arg2, boost::tup
les::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuple
s::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples:
:null_type, boost::tuples::null_type>::type> > boost::lambda::bind(const Arg1&,
const Arg2&) [with Arg1 = const int ()(const char*), Arg2 = boost::lambda::place
holder1_type]'
というエラーが出てしまいます。
環境はg++です。
489: 471 [sage] 2009/05/03(日) 18:03:35 AAS
先のソースは
× MyClass::get_10(const char * const)
○ MyClass::get_10(const char * const, const char * const)
× boost::lambda::bind(MyClass::get_10, boost::lambda::_1)
○ boost::lambda::bind(MyClass::get_10, boost::lambda::_1, boost::lambda::_2)
とすべきでした。失礼致しました。
それでもなお
x_error.cpp:81: error: invalid initialization of non-const reference of type 'co
nst int (&)(const char*, const char*)' from a temporary of type 'const int ()(co
nst char*, const char*)'
というエラーが出てしまいます。
非constな参照でテンポラリな何物かを捉えているという旨のようですが、
どうすれば回避できるのか分かりません。
どうかお知恵をお貸しください。
490: 471 [sage] 2009/05/03(日) 18:31:34 AAS
boost::lambda::bind
をboost::bindに書き換えたら問題無く動作しました。
個人的にはboost::bindライブラリとboost::lambdaライブラリを混在させるのは気持ち悪いので
出来ればboost::bindの箇所は無くして全てboost::lambda::bindにしたいのですが、
どうすれば良いでしょうか?
492(1): 471 [sage] 2009/05/03(日) 21:01:43 AAS
>>491491(1): デフォルトの名無しさん [sage] 2009/05/03(日) 20:36:47 AAS
3行でまとめてくれ
SpiritのSemantic Actionにlambda::bindで関数を結合して渡すとエラーになりますが、boost::bindならOKです。lambda::bindでも大丈夫にする方法を教えていただけないでしょうか?
494: 471 [sage] 2009/05/03(日) 21:36:52 AAS
>>493493(1): デフォルトの名無しさん [sage] 2009/05/03(日) 21:17:04 AAS
恐縮ですwww
func_geti_retiの引数の&が余分じゃないか?
int func_geti_reti(const int& num)
を
int func_geti_reti(const int num)
や
int func_geti_reti(int num)
にしてみましたが、
error: invalid initialization of non-const reference of type 'const int (&)(const char*)' from a temporary of type 'const int ()(const char*)'
と言われます。
私の見解では
関数の戻り値などのテンポラリオブジェクト(この場合はint)を
constな参照で束縛できるというC++の仕様がありますから、
このあたりが問題な訳ではなさそうだと思っております。
877(1): 471 [sage] 2009/08/07(金) 11:40:23 AAS
俺もboost::lambda::bindはboost::bindの完全上位互換だと思っていたが、
>>471
>>492
この辺で
boost::lambda::bindでは出来ないがboost::bindでは出来る例が
書かれているようだぞ。
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.041s