Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding complex typedef expressions [duplicate]

Tags:

c++

c++11

I'd like to know what this expression means??

typedef bool (*compareShapes)(const Shape* s1, const Shape* s2);

The Shape is a class.

like image 878
Younès Raoui Avatar asked Nov 23 '16 14:11

Younès Raoui


1 Answers

You should be using using-statements. It make the very same declaration easier to read:

using compareShapes = bool(*)(const Shape*, const Shape*);

See it now? the type is after the equal sign. It's a pointer to function type.

One could declare it like this too:

// function pointer type --v----------------------------------------------------v
using compareShapes =      std::add_pointer_t< bool(const Shape*, const Shape*) >;
//                    function signature  -----^------------------------------^

The function signature is composed like this:

<return-type>( <arguments...> )

When you add the pointer in it, it looks like this:

<return-type>(*)( <arguments...> )

And if you add the pointer name or type name here, add it after the star:

<return-type>(*typeOrPointerName)( <arguments...> )
like image 171
Guillaume Racicot Avatar answered Sep 19 '22 13:09

Guillaume Racicot