I am reading C++ Primer about how to define a set. As I look through cppreference about set, I feel confused about the class and constructor signature of set.
From the text:
bool compareIsbn(){....}
multiset<Sales_data, decltype(compareIsbn) *> bookstore(compareIsbn)
Text1:
To use our own operation, we must define the multiset with two types: the key type,
Sales_data, and the comparison type, which is a function pointer type that can point tocompareIsbn. When we define objects of this type, we supply a pointer to the operation we intend to use. In this case, we supply a pointer tocompareIsbn:
So from text1, it seems that I need to put a function pointer in set<Class A, a function pointer>.
But when I look up cppreference, the "signature"(not sure if it's the right word) is:
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class set;
Question1: So the second parameter should be a class?
Text2:
We can write
compareIsbninstead of&compareIsbnas the constructor argument because when we use the name of a function, it is automatically converted into a pointer if needed. We could have written&compareIsbnwith the same effect.
Also from cppreference constructor of set(I think the textbook is using the first constructor):
set();
explicit set( const Compare& comp,
const Allocator& alloc = Allocator() );
Question2: Doesn't the constructor require a function reference? From where it 's saying that it needs a function pointer?
Need some help to understand the "signature" , thanks in advance :D.
PS:
- the textbook uses
multisetas an example, but the "signature" ofsetandmultisetare quite similar. - I don't know what
const Allocator& alloc = Allocator()means, but I think maybe it's something get done automatically? I may ignore it for now.