1

I'm using boost::function for making function-references:

typedef boost::function<void (SomeClass &handle)> Ref;
someFunc(Ref &pointer) {/*...*/}

void Foo(SomeClass &handle) {/*...*/}

What is the best way to pass Foo into the someFunc? I tried something like:

someFunc(Ref(Foo));

1 Answer 1

5

In order to pass a temporary object to the function, it must take the argument either by value or by constant reference. Non-constant references to temporary objects aren't allowed. So either of the following should work:

void someFunc(const Ref&);
someFunc(Ref(Foo)); // OK, constant reference to temporary

void someFunc(Ref);
someFunc(Ref(Foo)); // OK, copy of temporary

void someFunc(Ref&);
someFunc(Ref(Foo)); // Invalid, non-constant reference to temporary
Ref ref(Foo);
someFunc(ref); // OK, non-constant reference to named object

By the way, calling the type Ref and the instance pointer when it's neither a reference nor a pointer could be a bit confusing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.