Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this weird function parameter?

Tags:

c++

today I came across this weird code:

auto rovoid_iterator
    (
        Construct ROII* const at,
        auto(ROII&)(auto(*)(Str&&)noexcept->void) ->void //WTF??
    ) -> void;

What the hell is this weird second parameter??

Thanks in advance!

like image 956
Noel2019 Avatar asked Aug 13 '19 17:08

Noel2019


People also ask

Can a function be a parameter in C++?

C++ has two ways to pass a function as a parameter. As you see, you can use either operation() or operation2() to give the same result.

What is it called when you pass a function as a parameter?

Function Call When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this: func(print);


Video Answer


2 Answers

Okay, let's deconstruct this abomination.

First, there's a inner type:

auto(*)(Str&&) noexcept -> void

This is a pointer to function taking a Str rvalue-reference as parameter. It's also a noexcept function.

Let's call that S

using S = auto(*)(Str&&) noexcept -> void;

Then the outer part of the parameter can be subtitued like that:

auto(ROII&)(S) -> void

As you stated in the comments, ROII is an empty macro. So in the end it reads like that:

auto(&)(S) -> void

That code appear to be a parameter which would be a reference to function that take a S which is and return void.

like image 88
Guillaume Racicot Avatar answered Oct 26 '22 02:10

Guillaume Racicot


Okay I asked the dev who wrote this:

auto(ROII&)(auto(*)(Str&&)noexcept->void) ->void 

Is a reference to a function which takes a function pointer as argument. This function pointer is a pointer because its okay to pass nullptr if you dont need it, but the first function must be passed thats why it is a reference. The second pointer is a pointer to a function wich is noexcept and takes a rvalue reference to a string as paremeter. ROII marks game ready functions.

like image 29
Noel2019 Avatar answered Oct 26 '22 01:10

Noel2019