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?

8
  • which edition? Did you check the errata if they mention it there? Commented Feb 14, 2018 at 11:15
  • 4th edition.didn't check, would do so. Commented Feb 14, 2018 at 11:16
  • @Default: Where is the errata in the book? Commented Feb 14, 2018 at 11:18
  • 1
    @Birbal: It's not in the book. If they knew the mistakes when they published the book, they wouldn't have made them. Google "the c++ programming language 4th edition errata". Commented Feb 14, 2018 at 11:20
  • See also this answer. It might be helpful Commented Feb 14, 2018 at 11:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Looking for a standard quote but can't actually find one. I think the types are different but end up being used the same.
they are not the same. It seems like in the context of function parameters are the same. godbolt.org/g/EhVV5Z
So what I just said then :)
yeah. It is very confusing. void call(F f) and void call2(F* f) have the same signature if F is a function type.
It is indeed a distinct and incomplete 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.