If Behavior
!= rome::target_is_mandatory
:
constexpr delegate() noexcept; // (1)
constexpr delegate(std::nullptr_t) noexcept; // (2)
delegate(delegate&& other) noexcept; // (3)
delegate(const delegate& other) = delete; // (4)
template<typename F>
delegate(F&& fnObject) noexcept(/*see below*/); // (5)
If Behavior
== rome::target_is_mandatory
:
delegate() = delete; // (1)
delegate(delegate&& other) noexcept; // (3)
delegate(const delegate& other) = delete; // (4)
template<typename F>
delegate(F&& fnObject) noexcept(/*see below*/); // (5)
Constructs a rome::delegate
.
- 1, 2 -- Creates an empty delegate.
Only provided ifBehavior
!=rome::target_is_mandatory
. - 3 -- Moves the target of other to
*this
. If other is empty,*this
will be empty after the call too. Leaves other in empty state after the move:- If
Behavior
!=rome::target_is_mandatory
:
After the move, other behaves as if default constructed. - If
Behavior
==rome::target_is_mandatory
:
After the move, other behaves as if being default constructed withBehavior
==rome::target_is_expected
and throws arome::bad_delegate_call
exception when called.
- If
- 4 --
rome::delegate
cannot be copied - 5 -- Creates a delegate with its target set to the passed function object.
LetT
bestd::decay_t<F>
, the type of the function object. The target is constructed byT(std::forward<F>(fnObject))
.- If
sizeof(T) <= sizeof(void*)
andalignof(T) <= alignof(void*)
:- The constructer is noexcept if
T(std::forward<F>(fnObject))
is noexcept. - The target is small object optimized, it is stored inside the
rome::delegate
. No dynamic allocation takes place.
- The constructer is noexcept if
- Otherwise:
- The constructor is not noexcept.
- The target is constructed in a dynamic allocated storage.
- If
other
Anotherrome::delegate
object used to initialize*this
.F
- template parameter
The type by which the function object target is passed.fnObject
The function object target used to initialize*this
.
See the code in examples/construct.cpp.
#include <iostream>
#include <rome/delegate.hpp>
void function() {
std::cout << "function\n";
}
struct C {
void method() {
std::cout << "method\n";
}
void const_method() const {
std::cout << "const method\n";
}
};
const auto fnObject = []() { std::cout << "function object\n"; };
int main() {
{
// const auto d = rome::delegate<void()>{&function}; // does not compile (1)
const auto d1 = rome::delegate<void()>{[]() { function(); }}; // (2)
const auto d2 = rome::delegate<void()>::create<&function>();
d1();
d2();
}
{
C object;
const auto d1 = rome::delegate<void()>{[&object]() { object.method(); }}; // (2)
const auto d2 = rome::delegate<void()>::create<C, &C::method>(object);
d1();
d2();
}
{
const C object;
const auto d1 = rome::delegate<void()>{[&object]() { object.const_method(); }}; // (2)
const auto d2 = rome::delegate<void()>::create<C, &C::const_method>(object);
d1();
d2();
}
{
const auto d1 = rome::delegate<void()>{fnObject};
const auto d2 = rome::delegate<void()>::create(fnObject);
d1();
d2();
}
}
- 1 -- not supported because less optimizable, needs to be wrapped by a function object
- 2 -- non function objects wrapped by a function object
Output:
function
function
method
method
const method
const method
function object
function object
- create - static
creates a newrome::delegate
instance with given target assigned