user0000001
user0000001

Reputation: 2233

Properly calling a parameterized std::function

I'm new to std::function and have been using it for trivial tasks. One of my projects passes an std::string into an std::function()– but now I would like to pass a rapidjson::Document instead.

Why does an std::string seamlessly work and rapidjson::Document does not? Is it because the document object is allocated on the stack and not the heap? Should I wrap this in a smart pointer?

I am struggling to understand the error itself and am hoping someone could explain what it means: call of an object of a class type without appropriate operator() or conversion function to pointer-to-function type

Here is my existing code:

std::function<void(const std::string&)> callback_;

...

bool set_callback(std::function<void(const std::string&)>&& callback) {
    callback_ = callback;
}

void some_event() {
    std::string serialize = serializer.to_string(an_object);

    callback_(data);
}

...

// The callback event
void the_callback(const std::string& data) {
    std::cout << data << std::endl;
}

The callback will now take an object document and the serialization will be done in another class entirely. So I would like my std::function to reflect this:

std::function<void(rapidjson::Document&)> callback_;

...

void some_event() {
    rapidjson::Document document = serializer.get_document();

    // This is where the error occurs
    callback_(document);
}

Upvotes: 0

Views: 77

Answers (1)

fish2000
fish2000

Reputation: 4435

According to the RapidJSON documentation, its Document and Value types are invariants that provide move-semantic operations but not copy-semantics:

  • Q. What is move semantics? Why?

  • A. Instead of copy semantics, move semantics is used in Value. That means, when assigning a source value to a target value, the ownership of source value is moved to the target value.

    Since moving is faster than copying, this design decision forces user to aware of the copying overhead.

Since you didn’t list the rest of the code changes you made when refitting this example to pass rapidjson::Document& references instead of std::string const& references, you should double-check that your set_callback(…) function is in sync with this change (and if possible, update your post to reflect this) and ensure that your functional signatures are move-semantic friendly (e.g. capable of receiving rapidjson::Document&& rvalue references).

Also, as Nichol Bolas mentioned above, the error message you posted could use some context (at which line did it occur; which compiler was in use, &c).

Upvotes: 1

Related Questions