Using Bind to produce a parameterless function results in error
-
22-03-2021 - |
문제
I am trying to figure out the proper use of std::bind with a boost::signal2 signal.
The error set I am getting with clang++ (from Xcode 4.2.1) is:
~/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.
They are primarily focused on two lines:
main.resize.connect(std::bind(resize, std::ref(main), _1, _2));
main.close.connect(std::bind(close, std::ref(main)));
Which use the function prototypes:
bool resize(Myron::Window &win, int &width, int &height);
bool close(Myron::Window &win);
and main is a Myron::Window&, and resize and close are respectfully:
bs2::signal<bool(int&,int&)> resize;
bs2::signal<bool(void)> close;
Everything I have read and have been told leads me to believe I am using these fairly correctly, so I am unsure what is the problem is.
I realize the first error is a problem with boost::bind making its way in here probably through the boost/signals2.hpp in the Myron.h file. The second error, is more of a mystery.
The full main source file is here:
#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;
}
Where the main source code repository showing everything is here: https://github.com/iaefai/Myron/tree/master/Myron
Any recommendations to try would be appreciated. I have tried making smaller test cases without success to bring out the issue.
해결책
The second issue is probably caused by overload ambiguity. Specifically, I suspect that the culprit is the system call close()
that sneaks into your scope via <iostream>
. Try enclosing your callbacks in a namespace and see if that helps. I'm surprised that clang++ isn't more helpful in this case, showing the overloads that cause ambiguity, it's usually praised for its very clear diagnostics.