从函数C中的Lambda函数返回值

时间:2018-07-05 10:28:52

标签: c++ lambda rabbitmq

我正在使用RabbiMQ(RPC),并且想从lambda函数返回一个值。我从main.cpp调用此函数,但返回值与预期的不一样(lambda函数内部的值与预期的一样)。正确的语法是什么?

我的代码:

bool RabbitMqHandler::sendResultToUserCountService(analyze_result result, Camera *cam)
{
    std:: string queueName = "userCountServiceReceiveRPC" + std::to_string(cam->getUserGroupId());

    const std::string correlation("2");

    SimplePocoHandler handler("localhost", 5672);

    AMQP::Connection connection(&handler, 
                                AMQP::Login("localhost","123456!"),"/");

    bool isExist;
    AMQP::Channel channel(&connection);
    AMQP::QueueCallback callback = [&](const std::string &name,
                                       int msgcount,
                                       int consumercount)
    {

         ProtobufLPR::CarResult carResult;

         carResult.set_licensenumber(result.LicenseNumber);
         carResult.set_analyzetime(result.Date);

         std::string buf;
         carResult.SerializeToString(&buf);

         AMQP::Envelope env(buf);
         env.setCorrelationID(correlation);
         env.setReplyTo(name);
         channel.publish("", queueName, env);
         std::cout << "Requesting " << result.LicenseNumber << std::endl;
    };

    channel.declareQueue(AMQP::exclusive).onSuccess(callback);

    auto receiveCallback = [&](const AMQP::Message &message,
                               uint64_t deliveryTag,
                               bool redelivered) ->bool
    {
        if(message.correlationID() != correlation)
            return 1;

        std::cout<<"Got " << message.message() <<std::endl;
        handler.quit();
        istringstream(message.message()) >> isExist;
        return isExist;
      };

      channel.consume("", AMQP::noack).onReceived(receiveCallback);
      handler.loop();
}

1 个答案:

答案 0 :(得分:0)

这里是从lambda函数返回值的示例。 Live demo

factorization_nb: 4.2 ms per iteration
factorization_orig: 460 ms per iteration (110x speedup)