std::bindとboost::signal2信号の適切な使用法を理解しようとしています。
clang ++(Xcode 4.2.1から)で発生するエラーセットは次のとおりです。
~/Projects/Myron/Myron/main.cpp:29:59: error: reference to '_1' is ambiguous [3]
main.resize.connect(std::bind(resize, std::ref(main), _1, _2));
^
/Developer/usr/bin/../lib/c++/v1/functional:1496:18: note: candidate found by name lookup is 'std::__1::placeholders::_1' [3]
extern __ph<1> _1;
^
/Developer/SDKs/MacOSX10.7.sdk/usr/local/include/boost/bind/placeholders.hpp:55:15: note: candidate found by name lookup is '<anonymous namespace>::_1' [3]
boost::arg<1> _1;
^
~/Projects/Myron/Myron/main.cpp:30:24:{30:24-30:33}: error: no matching function for call to 'bind' [3]
main.close.connect(std::bind(close, std::ref(main)));
^~~~~~~~~
/Developer/usr/bin/../lib/c++/v1/functional:1789:1: note: candidate template ignored: couldn't infer template argument '_F' [3]
bind(_F&& __f, _BoundArgs&&... __bound_args)
^
/Developer/usr/bin/../lib/c++/v1/functional:1798:1: note: candidate template ignored: couldn't infer template argument '_R' [3]
bind(_F&& __f, _BoundArgs&&... __bound_args)
^
2 errors generated.
それらは主に2つのラインに焦点を合わせています:
main.resize.connect(std::bind(resize, std::ref(main), _1, _2));
main.close.connect(std::bind(close, std::ref(main)));
関数プロトタイプを使用するもの:
bool resize(Myron::Window &win, int &width, int &height);
bool close(Myron::Window &win);
メインはMyron::Window&であり、サイズ変更とクローズは丁重に行われます。
bs2::signal<bool(int&,int&)> resize;
bs2::signal<bool(void)> close;
私が読んだり言われたりしたことはすべて、私がこれらをかなり正しく使用していると私に信じさせてくれるので、何が問題なのかわかりません。
最初のエラーは、おそらくMyron.hファイルのboost/signals2.hppを介してここに入るboost::bindの問題であることに気付きました。2番目のエラーは、もっと謎です。
完全なメインソースファイルはここにあります:
#include <iostream>
#include <functional>
#include <string>
#include <type_traits>
#include "Myron.h"
bool setup();
bool resize(Myron::Window &win, int &width, int &height);
bool close(Myron::Window &win);
bool setup()
{
using namespace std::placeholders;
std::cout << "setup()" << std::endl;
Myron::Window &main = Myron::createWindow(640, 480);
main.resize.connect(std::bind(resize, std::ref(main), _1, _2));
main.close.connect(std::bind(close, std::ref(main)));
return true;
}
bool close(Myron::Window &win)
{
std::cout << "Window Closed" << std::endl;
return true;
}
bool resize(Myron::Window &win, int &width, int &height)
{
std::cout << "Resize: " << width << ", " << height << std::endl;
return true;
}
int main(int argc, char**argv)
{
std::cout << "Initialize..." << std::endl;
Myron::Init(setup);
std::cout << "Done Initialize." << std::endl;
}
すべてを表示するメインのソースコードリポジトリはここにあります:https ://github.com/iaefai/Myron/tree/master/Myron
試してみることをお勧めします。私は問題を引き出すために成功せずに小さなテストケースを作ってみました。