3

I am reading The C++ Programming Language by Bjarne Stroustrup. It somewhere uses using keyword to make function-pointer datatypes P1 and P2 like this:

using P1 = int(∗)(int∗);

using P2 = void(∗)(void);

But then it uses using keyword to make another function-pointer datatype:

using CFT = int(const void∗, const void∗);             -(1)

then it uses CFT to declare a function-pointer and passes it in some ssort function:

void ssort(void∗ base, siz e_t n, size_t sz, CFT cmp);

My question is if it is making a function-pointer datatype using "using" then shouldn't line-(1) be:

using CFT=int(*)(const void*, const void*); 

rather than what it actually is?

Birbal
  • 61
  • 7

1 Answers1

5

In C and C++, the (*) is optional here.

Yes, this is confusing. It's an oddity regarding function pointer types.

It would have been better had the author stuck to one of the two possible syntaxen.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Looking for a standard quote but can't actually find one. I think the types are different but end up being used the same. – Lightness Races in Orbit Feb 14 '18 at 11:27
  • they are not the same. It seems like in the context of function parameters are the same. https://godbolt.org/g/EhVV5Z – bolov Feb 14 '18 at 11:39
  • So what I just said then :) – Lightness Races in Orbit Feb 14 '18 at 11:40
  • yeah. It is very confusing. `void call(F f)` and `void call2(F* f)` have the same signature if `F` is a function type. – bolov Feb 14 '18 at 11:42
  • It's maddening - I do try to avoid naked function pointers, probably at the expense of a bit of runtime overhead with `std::function` ahem :D – Lightness Races in Orbit Feb 14 '18 at 11:42
  • 1
    It is indeed a distinct and incomplete [function type](https://timsong-cpp.github.io/cppwp/n4659/dcl.fct#def:function_type). And it has its uses and oddities. I personally like that one may use the original `CFT` to make pointer semantics explicit, i.e. `CFT*`. Not to mention being able to use it with `std::function`, as you already mentioned. – StoryTeller - Unslander Monica Feb 14 '18 at 12:08