cyclonedds-cxx icon indicating copy to clipboard operation
cyclonedds-cxx copied to clipboard

Creating ReadCondition with lambda as "functor" arg

Open NoeSechet opened this issue 3 years ago • 1 comments

I was trying to create a ReadCondition object using this constructor:

    template <typename FUN>
    TReadCondition(const dds::sub::AnyDataReader& dr, const dds::sub::status::DataState& status, FUN functor);

In your tests you always create a separate class, overload the "()" operator and give that class as the 3rd argument to the constructor.

I have been trying to give it a lambda like so:

        ::dds::sub::cond::ReadCondition read_condition(
            this->req_data_reader(),
            ::dds::sub::status::DataState::any(),
            [this]() { /* DO SOMETHING */ }
        );

But I get this: note: candidate expects 0 arguments, 1 provided

I could guess it was because the lambda is called with an argument in the background, is there a way to use a lambda without having to add an argument?

NOTE: This fixed my problem but I wanted to find a "proper" way to do it:

        ::dds::sub::cond::ReadCondition read_condition(
            this->req_data_reader(),
            ::dds::sub::status::DataState::any(),
            [this](::dds::core::cond::Condition c /* unused */) { /* DO SOMETHING */ }
        );

NoeSechet avatar Jan 28 '22 11:01 NoeSechet

@NoeSechet it seems perfectly reasonable to allow a lambda function without an argument via some overloading tricks. But I am no C++ expert and don't know what the consequences would be exactly. Fortunately, I can usually count on @e-hndrks providing some good insights, hopefully he has some time to comment on this, too.

eboasson avatar Jan 31 '22 16:01 eboasson