Is it possible to use this type of containers to ...
Regardless of how this sentence continues: No, it is not possible to use this type of containers. Specifically, element type of no standard container can be a reference. Reference types do not satisfy the requirements that containers have for their element type (at least not when using the standard allocator).
if not, is it the only way to use function pointer?
No, function pointer is not the only way, but it is a way that works.
Other alternatives are function objects such as an erasing function wrapper such as std::function
, or a reference wrapper such as std::reference_wrapper
.
i just thought there is no need to make dereference.
If you mean syntactically, then I have good news that make your concern irrelevant: There is no need to explicitly indirect through a pointer to function. The indirection is implicit just like with function references. Their call syntax is identical Example:
float(&ref)(float) = func_imp;
float(*ptr)(float) = func_imp;
ref(42.);
ptr(42.);
As such, you needen't worry.
If you are talking about having to indirect through the pointer at runtime at the cost of performance, I have bad news that make your concern irrelevant: References are also a form of indirection just as much as pointers are. They are (typically) not an optimisation.