1

I am fairly new to c++. I have some experience with programming in general and in C in particular though. I have a c++ project written by someone else which I am trying to comprehend at the moment, and it includes several variable declarations in this format:

 uint64_t(*const col_timestamp)(const uint8_t* col_buf);

I fail to wrap my head around what this means. Is it even a variable declaration? I would understand the two seperate declarations of the constant pointer col_timestamp pointing to a variable of type uint64_t and the pointer col_buf to a variable of type const uint8_t like this:

uint64_t * const col_timestamp;
const uint8_t * col_buf;

But I don't think that is what it means since I dont see a reason to write it in this way then. Help would be very appreciated since I am kind of stuck here.

I am sorry if this is a duplicate question of sorts, but I simply don't know what to search for, and I guess this is very simple to answer for someone knowledgeable in c++.

Thanks in advance!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
hoefes96
  • 13
  • 3
  • Welcome to Stackoverflow. Yes you stumbled into the big mess of C function pointer syntax which is also supported in C++. Fortunately you don't need them in C++ anymore, since your have things like `std::function` and template parameters (like `std::copy_if` uses). However the linked duplicate should answer most of your questions, now that you know what that actually is. – Superlokkus Oct 06 '20 at 11:08
  • Thanks for the quick response! I see, so this is the declaration of a function pointer which takes a pointer as a parameter? This is kind of weird then, because the same project also uses quite a lot of templated code. Do you have any idea what could be the benefit of using function pointers over function templates? – hoefes96 Oct 06 '20 at 11:18
  • In my opinion there is no benefit, and in fact compared to function template parameters, there is no performance benefit whatsoever, actually AFAIK function template parameters are beneficial to runtime performance. That said for compile time bound function parameters, if you want to bind functions at runtime, `std::function` is the way to go. – Superlokkus Oct 06 '20 at 12:13

1 Answers1

1

This

uint64_t(*const col_timestamp)(const uint8_t* col_buf);

is a declaration of a constant pointer to function that has the return type uint64_t and one parameter of the type const uint8_t *.

For example if you have a function declared like

uint64_t func_col_timestamp( const uint8_t *col_buf );

You could declare and initialize a pointer to the function like

uint64_t(*const col_timestamp)(const uint8_t* col_buf) = func_col_timestamp;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I see, thank you! but should the initialization not be uint64_t(*const col_timestamp)(const uint8_t* col_buf) = &func_col_timestamp since we are initializing a pointer? – hoefes96 Oct 06 '20 at 11:40
  • @hoefes96 A function designator used in expressions like an initializer is implicitly converted to pointer to function. So using the operator & is redundant. – Vlad from Moscow Oct 06 '20 at 11:45