std::function<void(bool, std::string)>
时间: 2023-11-07 07:54:48 浏览: 39
引用中给出了一个使用function_traits模板类的例子,该模板类用于从std::function中提取信息。根据这个例子,我们可以得出以下结论:
对于std::function<void(bool, std::string)>,function_traits的value将返回2,表示有两个参数。
result_type将返回void,表示返回类型为void。
get<0>::type将返回bool,表示第一个参数的类型为bool。
get<1>::type将返回std::string,表示第二个参数的类型为std::string。
因此,std::function<void(bool, std::string)>的信息如下:
- 有两个参数:bool和std::string
- 返回类型为void
相关问题
/usr/bin/ld: CMakeFiles/global_planning_node.dir/src/global_planning_node.cpp.o: in function main.cold': global_planning_node.cpp:(.text.unlikely+0x273): undefined reference to tf::TransformListener::~TransformListener()' /usr/bin/ld: CMakeFiles/global_planning_node.dir/src/global_planning_node.cpp.o: in function main': global_planning_node.cpp:(.text.startup+0xc64): undefined reference to tf::Transformer::DEFAULT_CACHE_TIME' /usr/bin/ld: global_planning_node.cpp:(.text.startup+0xc92): undefined reference to tf::TransformListener::TransformListener(ros::Duration, bool)' /usr/bin/ld: global_planning_node.cpp:(.text.startup+0xd7a): undefined reference to tf::Transformer::lookupTransform(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ros::Time const&, tf::StampedTransform&) const' /usr/bin/ld: global_planning_node.cpp:(.text.startup+0xe74): undefined reference to tf::TransformListener::~TransformListener()' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/global_planning_node.dir/build.make:246: /home/juan/catkin_ws/devel/.private/putn/lib/putn/global_planning_node] Error 1 make[1]: *** [CMakeFiles/Makefile2:207: CMakeFiles/global_planning_node.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... /usr/bin/ld: CMakeFiles/local_obs_node.dir/src/local_obs.cpp.o: in function rcvVelodyneCallBack(sensor_msgs::PointCloud2<std::allocator<void> > const&)': local_obs.cpp:(.text+0xa0b): undefined reference to tf::Transformer::waitForTransform(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ros::Time const&, ros::Duration const&, ros::Duration const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) const' /usr/bin/ld: local_obs.cpp:(.text+0xc74): undefined reference to tf::TransformListener::transformPoint(std::cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, geometry_msgs::PointStamped<std::allocator<void> > const&, geometry_msgs::PointStamped<std::allocator<void> >&) const' /usr/bin/ld: CMakeFiles/local_obs_node.dir/src/local_obs.cpp.o: in function main.cold': local_obs.cpp:(.text.unlikely+0x37d): undefined reference to tf::TransformListener::~TransformListener()' /usr/bin/ld: CMakeFiles/local_obs_node.dir/src/local_obs.cpp.o: in function main':local_obs.cpp:(.text.startup+0x62a): undefined reference to tf::Transformer::DEFAULT_CACHE_TIME' /usr/bin/ld: local_obs.cpp:(.text.startup+0x64d): undefined reference to tf::TransformListener::TransformListener(ros::Duration, bool)' /usr/bin/ld: local_obs.cpp:(.text.startup+0x6dc): undefined reference to tf::TransformListener::~TransformListener()' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/local_obs_node.dir/build.make:246: /home/juan/catkin_ws/devel/.private/putn/lib/putn/local_obs_node] Error 1 make[1]: *** [CMakeFiles/Makefile2:612: CMakeFiles/local_obs_node.dir/all] Error 2 make: *** [Makefile:141: all] Error 2解释编译时出现这个问题的原因,并说说如何解决
这个编译错误是因为链接器找不到tf库的符号引用。tf库是一个用于处理坐标变换的库,它提供了TransformListener和Transformer等类。
要解决这个问题,你需要确保tf库正确地链接到你的项目中。以下是一些可能的解决方法:
1. 确保tf库已经正确地安装在你的系统中。如果你使用的是ROS,可以通过运行以下命令来安装tf库:
```
sudo apt-get install ros-<distro>-tf
```
这里的 `<distro>` 是你正在使用的ROS发行版,例如`melodic`或`noetic`。
2. 确保在你的CMakeLists.txt文件中正确地添加了tf库的依赖项。你可以在`find_package`命令后添加以下行来链接tf库:
```
target_link_libraries(your_target_name ${catkin_LIBRARIES} tf)
```
这里的 `your_target_name` 是你的目标可执行文件的名称。
3. 如果你的项目使用了其他依赖项,例如tf2或tf2_ros,你可能还需要添加它们的链接库。例如:
```
target_link_libraries(your_target_name ${catkin_LIBRARIES} tf tf2 tf2_ros)
```
4. 确保你的源代码文件中包含了正确的头文件引用。对于tf库,你可能需要包含`<tf/transform_listener.h>`和`<tf/transform_broadcaster.h>`。
尝试以上方法后,重新编译你的项目,应该能够解决链接错误。如果问题仍然存在,请确保在编译过程中没有其他错误或警告,并仔细检查你的代码和编译配置。
using clock_type = std::chrono::system_clock; struct message { clock_type::time_point when; std::function<void()> callback; std::string param; }; class message_loop { public: message_loop(): _stop(false) { // } message_loop(const message_loop&) = delete; message_loop& operator=(const message_loop&) = delete; void run() { while (!_stop) { auto msg = wait_one(); msg.callback(); } } void quit() { post({clock_type::now(), this{ _stop = true; } }); } void post(std::function<void()> callable) { post({clock_type::now(), std::move(callable)}); } void post(std::function<void()> callable, std::chrono::milliseconds delay) { post({clock_type::now() + delay, std::move(callable)}); } private: struct msg_prio_comp { inline bool operator() (const message& a, const message& b) { return a.when > b.when; } }; using queue_type = std::priority_queue<message, std::vector<message>, msg_prio_comp>; std::mutex _mtx; std::condition_variable _cv; queue_type _msgs; bool _stop; void post(message msg) { auto lck = acquire_lock(); _msgs.emplace(std::move(msg)); _cv.notify_one(); } std::unique_lockstd::mutex acquire_lock() { return std::unique_lockstd::mutex(_mtx); } bool idle() const { return _msgs.empty(); } const message& top() const { return _msgs.top(); } message pop() { auto msg = top(); _msgs.pop(); return msg; } message wait_one() { while (true) { auto lck = acquire_lock(); if (idle()) _cv.wait(lck); else if (top().when <= clock_type::now()) return pop(); else { _cv.wait_until(lck, top().when); // 可能是新消息到达,再循环一次看看 } } } }; int main(int argc, char *argv[]) { using namespace std; using namespace std::chrono; message_loop *pLoop = new message_loop; thread th(pLoop{ pLoop->run(); }); cout << "POST 1"<<endl;; pLoop->post({ cout << "1"<<endl; }); cout << "POST 2"<<endl;; pLoop->post({ cout << "2"<<endl; }, milliseconds(500)); cout << "POST 3"<<endl;; pLoop->post({ cout << "3"<<endl; }); cout << "POST 4"<<endl;; pLoop->post({ cout << "4"<<endl; }, milliseconds(1000)); this_thread::sleep_for(milliseconds(1500)); // pLoop->quit(); cout << "Quit"<<endl; th.join(); cout << "here"<<endl; } 请优化一下,可以传参
可以将 message 结构体中的 param 字段用于传递参数,并相应地修改 post 函数和 wait_one 函数。具体代码如下:
```
using clock_type = std::chrono::system_clock;
struct message {
clock_type::time_point when;
std::function<void(const std::string&)> callback;
std::string param;
};
class message_loop {
public:
message_loop() : _stop(false) {}
message_loop(const message_loop&) = delete;
message_loop& operator=(const message_loop&) = delete;
void run() {
while (!_stop) {
auto msg = wait_one();
msg.callback(msg.param);
}
}
void quit() {
post({clock_type::now(), [](const std::string&) {}}, "");
_stop = true;
}
void post(std::function<void(const std::string&)> callable, const std::string& param) {
post({clock_type::now(), std::move(callable), param});
}
void post(std::function<void(const std::string&)> callable, std::chrono::milliseconds delay, const std::string& param) {
post({clock_type::now() + delay, std::move(callable), param});
}
private:
struct msg_prio_comp {
inline bool operator() (const message& a, const message& b) {
return a.when > b.when;
}
};
using queue_type = std::priority_queue<message, std::vector<message>, msg_prio_comp>;
std::mutex _mtx;
std::condition_variable _cv;
queue_type _msgs;
bool _stop;
void post(message msg) {
auto lck = acquire_lock();
_msgs.emplace(std::move(msg));
_cv.notify_one();
}
std::unique_lock<std::mutex> acquire_lock() {
return std::unique_lock<std::mutex>(_mtx);
}
bool idle() const {
return _msgs.empty();
}
const message& top() const {
return _msgs.top();
}
message pop() {
auto msg = top();
_msgs.pop();
return msg;
}
message wait_one() {
while (true) {
auto lck = acquire_lock();
if (idle()) {
_cv.wait(lck);
} else if (top().when <= clock_type::now()) {
auto msg = pop();
return msg;
} else {
_cv.wait_until(lck, top().when);
// 可能是新消息到达,再循环一次看看
}
}
}
};
int main(int argc, char *argv[]) {
using namespace std;
using namespace std::chrono;
message_loop *pLoop = new message_loop;
thread th(pLoop, &message_loop::run);
pLoop->post([](const std::string& param) { cout << "1 " << param << endl; }, "hello");
pLoop->post([](const std::string& param) { cout << "2 " << param << endl; }, milliseconds(500), "world");
pLoop->post([](const std::string& param) { cout << "3 " << param << endl; }, "foo");
pLoop->post([](const std::string& param) { cout << "4 " << param << endl; }, milliseconds(1000), "bar");
this_thread::sleep_for(milliseconds(1500));
pLoop->quit();
th.join();
}
```