Skip to content

Latest commit

 

History

History
123 lines (103 loc) · 3.97 KB

constructor.md

File metadata and controls

123 lines (103 loc) · 3.97 KB

rome::delegate<Ret(Args...), Behavior>:: delegate

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 if Behavior != 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 with Behavior == rome::target_is_expected and throws a rome::bad_delegate_call exception when called.
  • 4 -- rome::delegate cannot be copied
  • 5 -- Creates a delegate with its target set to the passed function object.
    Let T be std::decay_t<F>, the type of the function object. The target is constructed by T(std::forward<F>(fnObject)).
    • If sizeof(T) <= sizeof(void*) and alignof(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.
    • Otherwise:
      • The constructor is not noexcept.
      • The target is constructed in a dynamic allocated storage.

Parameters

  • other
    Another rome::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.

Examples

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

See also

  • create - static
    creates a new rome::delegate instance with given target assigned