In how many ways can a function be invoked(called) in C++?

c++, function

Solution

Arbitrary functions can be invoked:

using `f(arguments...)` notation

via a pointer to the function (whether member or non-)

via a `std::function` - (will check the implementation's left unspecified, though I'd expect it to use a pointer to function or pointer to member function under the covers so no new language features)

Class-specific functions are also invoked in certain situations:

constructors are invoked when objects are created on the stack, and when static/global or thread-specific objects or dynamically-allocated objects are dynamically initialised, or with placement `new`, and as expressions are evaluated

destructors are invoked when objects leave scope, are `delete`d, threads exit, temporaries are destroyed, and when the destructor is explicitly called ala `x.~X()`

all manner of operators (`[]`, `+=`, `==`, `<` etc.) may be invoked during expression evaluation

Arbitrary non-member functions may be run by:

functions may be run due to earlier `std::atexit()` or `std::at_quick_exit()` calls, and if they throw `std::terminate` may run

thread creation and asynchronous signals (again the interfaces accept pointer to functions, and there's no reason to think any implementation has or would use any other technique to achieve dispatch)

Specific functions are triggered in very specific situations:

`main()` is executed by the runtime

`std::unexpected`, `std::unexpected_handler`, `std::terminate` are invoked when dynamic exception specifications are violated

It's also possible to use `setjmp` and `longjmp` to "jump" back into a function... not quite the same thing as calling it though.

Though not truly "C++", it's also possible to arrange function execution using inline assembly language / linked assembler, writing to executable memory.

Problem

I know of one way to call a function : ``` func(x, y); ``` Are there more ways to call a function?

Original source

Related problems