--- title: "delegate" weight: 1 --- etl::delegate is a type-safe, generic, and efficient delegate implementation. A delegate is essentially a type-safe function pointer. It can be used to encapsulate a callable unit (like a function, a function pointer, member function, or a callable object like a functor or a lambda function) and call it later. etl::delegate is similar in a lot of ways to std::function_ref introduced with C++26. The delegate functions may be defined at compile time and/or runtime, depending on the function type. Most delegates can only be constructed via a create function, except lambdas and functors, which can be created by the delegate constructor. This may be used to implement a platform abstraction layer that allows an embedded application to interface with multiple hardware platforms. Here's a high-level overview of how etl::delegate works: Creation: When you create a delegate, you specify the function signature that it should match. This is done using template parameters. For example, etl::delegate creates a delegate that can hold references to functions that take an integer as an argument and return void. Assignment: You can assign a function to the delegate using the create method. The function you assign must match the delegate's function signature. For example, if you have a delegate of type etl::delegate, you could assign a function void myFunction(int) to it like this: auto myDelegate = etl::delegate::create(); Invocation: Once a function has been assigned to the delegate, you can call that function through the delegate just like you would call a regular function. For example, if myDelegate is a delegate that holds a reference to myFunction, you could call myFunction through the delegate like this: myDelegate(123); ____________________________________________________________________________________________________ Notes: <=20.21.0 This class only supports >=C++11. >=20.22.0 This class is automatically selects C++03 or C++11 versions based on the value of ETL_USING_CPP11 The C++03 variant only supports delegates taking 0 or 1 parameter. etl::delegate is non-owning. You cannot use lambdas that capture variables that will have gone out of scope when the delegate is called. This would result in dangling references. Lambdas are not copied; The delegate merely keeps pointers to lambdas. This also applies to functors. >=20.40.0 Move constructors from lambdas or functors are disabled, thereby stopping a delegate being created from a temporary defined in aparameter. >=20.42.0 etl::delegate is noexcept ____________________________________________________________________________________________________ etl::delegate 20.22.0 For C++03 etl::delegate etl::delegate For the examples below, assume the following definitions. class Test { public: int member_function(int); static int member_static(int); int operator()(int); int operator()(int) const; }; Test test; int global(int); auto lambda = [](int i){ return i; }; ____________________________________________________________________________________________________ Public types return_type TReturn 20.43.0 argument_types etl::type_list 20.43.0 ____________________________________________________________________________________________________ Run time construction Lambdas etl::delegate d(lambda); etl::delegate d = lambda; etl::delegate d = etl::delegate::create(lambda); d.set(lambda); Functors etl::delegate d(test); etl::delegate d = test; etl::delegate d = etl::delegate::create(test); d.set(); Member functions etl::delegate d = etl::delegate::create(test); d.set(test); d.set(); ____________________________________________________________________________________________________ Compile time construction Global functions auto d = etl::delegate::create(); Member functions (if the instance is known at compile time) auto d = etl::delegate::create(); // Deprecated auto d = etl::delegate::create(); // New Functors (if the instance is known at compile time) auto d = etl::delegate::create(); auto d = etl::delegate::create(); Note: These are disabled for GCC <= v8.1 as it generates an 'Internal Compiler Error'. Static member functions auto d = etl::delegate::create(); ____________________________________________________________________________________________________ Constexpr Most delegates can be declared as constexpr. (C++11 and above) static Test test; constexpr auto d = etl::delegate::create(); All create functions are constexpr under C++14. All create functions are [[nodiscard]] under C++17. ____________________________________________________________________________________________________ Calling the delegate The delegate may be called as a function with the defined parameter signature. etl::delegate d; int r = d(3); ____________________________________________________________________________________________________ Operations ETL_CONSTEXPR14 delegate() Constructs an uninitialised delegate. ____________________________________________________________________________________________________ ETL_CONSTEXPR14 delegate(const delegate& other) Copy constructs a delegate. ____________________________________________________________________________________________________ ETL_CONSTEXPR14 TReturn operator()(TParams... args) const ETL_NOEXCEPT Executes the delegate. constexpr from 20.42.1 ____________________________________________________________________________________________________ 20.17.0 ETL_CONSTEXPR14 bool call_if(TParams... args) const ETL_NOEXCEPT For delegates returning void. Calls the delegate if valid. Returns true if valid, otherwise false. constexpr from 20.42.2 ETL_CONSTEXPR14 etl::optional call_if(TParams... args) const ETL_NOEXCEPT For delegates returning TReturn. Calls the delegate if valid. Returns a valid etl::optional containing the return value, if valid. constexpr from 20.42.2 ____________________________________________________________________________________________________ 20.17.0 template ETL_CONSTEXPR14 TReturn call_or(TAlternative alternative, TParams... args) const ETL_NOEXCEPT Calls the delegate if valid, otherwise calls alternative. constexpr from 20.42.2 template ETL_CONSTEXPR14 TReturn call_or(TParams... args) const ETL_NOEXCEPT Calls the delegate if valid, otherwise calls Method. ____________________________________________________________________________________________________ void clear() ETL_NOEXCEPT Sets the delegate back to the uninitialised state. ____________________________________________________________________________________________________ ETL_NODISCARD ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT ETL_CONSTEXPR14 operator bool() const Returns true if the delegate has been initialised, otherwise false. ____________________________________________________________________________________________________ ETL_CONSTEXPR14 operator =() ETL_NOEXCEPT Assigns from another delegate or lambda. ____________________________________________________________________________________________________ ETL_CONSTEXPR14 operator ==() ETL_NOEXCEPT ETL_CONSTEXPR14 operator !=() ETL_NOEXCEPT Compares delegates. ____________________________________________________________________________________________________ make_delegate For C++17 and above. 20.39.5 ____________________________________________________________________________________________________ template constexpr auto make_delegate() noexcept Make a delegate from a free function. Returns etl::delegate::create(); ____________________________________________________________________________________________________ template constexpr auto make_delegate(TLambda& instance) noexcept Make a delegate from a functor or lambda function. Returns etl::delegate(instance); ____________________________________________________________________________________________________ template constexpr auto make_delegate() noexcept Make a delegate from a functor, compile time. Returns etl::delegate::create(); ____________________________________________________________________________________________________ template constexpr auto make_delegate() noexcept Make a delegate from a member function at compile time. Returns etl::delegate::create(); ____________________________________________________________________________________________________ template constexpr auto make_delegate() noexcept Make a delegate from a const member function at compile time. Returns etl::delegate::create(); ____________________________________________________________________________________________________ template constexpr auto make_delegate(T& instance) noexcept Make a delegate from a member function at run time. Returns etl::delegate::create(instance); ____________________________________________________________________________________________________ template constexpr auto make_delegate(const T& instance) noexcept Make a delegate from a member function at run time. Returns etl::delegate::create(instance);