std::bind通过对象成员函数创建线程时不能使用默认参数

有如此代码:

int Viewer::launch(bool resizable = true, bool fullscreen = false, const std::string &name = "libigl viewer", int width = 0, int height = 0)
{
    ...
}
 
...
 
// GUI是一个指向Viewer对象的指针
GUIThread = std::make_shared<std::thread>(std::bind(&igl::opengl::glfw::Viewer::launch, GUI, true, false));

编译不过去,原因是上面的launch函数修改过,原版是没有const std::string &name以及后面的三个参数的。本以为后面的全时默认参数,不写也没关系,验证发现必须写成这样:

GUIThread = std::make_shared<std::thread>(std::bind(&igl::opengl::glfw::Viewer::launch, GUI, true, false, GUI_name, 0, 0));

可以看出,默认参数对于C++来说真的就是一个语法糖性质的东西,只有在你通过声明这些默认参数的.h文件直接调用时才生效,其他情况应该都是没用的。

  • 最后更改: 2019/07/04 07:54