小编use*_*853的帖子

C++ 1y没有从std :: bind到std :: function的可行转换

我试图存储一个前向功能std::function.如果我使用std::bind,我收到错误消息no viable conversion from ....如果我使用lambda,它编译好了.

这是示例代码

#include <functional>

template<typename Handler>void func1(int a, Handler&& handler) {}
template<typename Handler>void func2(Handler&& handler)
{
    // this line compile fine
    std::function<void ()> funcA = [handler = std::move(handler)]() { func1(1, std::move(handler)); };
    // this line got compile error
    std::function<void ()> funcB = std::bind(func1<Handler>, 1, std::move(handler));
}

int main()
{
    func2(&main); // this just a sample, I am using functor as argument in real code
}
Run Code Online (Sandbox Code Playgroud)

尝试g ++ --std = …

c++ c++11 c++14

20
推荐指数
1
解决办法
4397
查看次数

glViewport在Qt5.4 QOpenGLWidget :: resizeGL上不起作用

我把glViewport放在QOpenGLWidget :: resizeGL覆盖的虚函数中,它实际上被调用并且set viewport只使用了widget空间的一部分.但它没有任何效果,内容仍然绘制到全尺寸的小部件.我错过了什么吗?

这是我的代码,

mywidget.h:

class MyWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget* parent = NULL);

protected:
    virtual void initializeGL();
    virtual void paintGL();
    virtual void resizeGL(int w, int h);

private:
    QOpenGLShaderProgram program;
};
Run Code Online (Sandbox Code Playgroud)

和mywidget.cpp:

MyWidget::MyWidget(QWidget* parent) : QOpenGLWidget(parent)
{
}

static const GLfloat squareVertices[] = {
    -1.0f, -1.0f,
     1.0f, -1.0f,
    -1.0f,  1.0f
};

void MyWidget::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    program.addShaderFromSourceCode(QOpenGLShader::Vertex,
        "attribute highp vec4 vec;\n"
        "void main(void)\n"
        "{\n"
        "    gl_Position = vec;\n"
        "}");
    program.addShaderFromSourceCode(QOpenGLShader::Fragment, …
Run Code Online (Sandbox Code Playgroud)

opengl qt5.4

8
推荐指数
1
解决办法
967
查看次数

boost::asio::bind_executor() 行为,是有意的吗?

我想在一个 io_context 中运行服务并在另一个 io_context 中执行业务逻辑。但首先我想了解更多有关重新调度行为的信息,因此我编写了一些实验代码。这是我的第一个版本(不使用 boost::asio::bind_executor())

boost::asio::io_context context1;
boost::asio::io_context context2;

// for simplicity I use post here, it will be socket operation in real code
boost::asio::post(context1, [&context2]()
    { boost::asio::post(context2, []() { printf("job finished"); }); });

// notice I didn't run context1 here
context2.run();
Run Code Online (Sandbox Code Playgroud)

context1 未运行,在本例中没有作业发布到 context2。所以 context2.run() 立即返回。当然,屏幕上没有打印任何内容。

然后这是使用 boost::asio::bind_executor() 的第二个版本

boost::asio::io_context context1;
boost::asio::io_context context2;

boost::asio::post(context1, boost::asio::bind_executor(context2, []()
    { printf("job finished"); }));

// notice I didn't run context1 here
context2.run();
Run Code Online (Sandbox Code Playgroud)

在这种情况下,进程永远停留在 context2.run() 中,等待待处理的作业被交付。

因此手写的包装器代码和 boost::asio::bind_executor() 的行为不同。这是错误还是故意的?建议采用哪一种方式?

提前致谢

c++ boost boost-asio

5
推荐指数
1
解决办法
782
查看次数

标签 统计

c++ ×2

boost ×1

boost-asio ×1

c++11 ×1

c++14 ×1

opengl ×1

qt5.4 ×1