mardi 13 juin 2023

What are the new ways to pass functions as template arguments in C++11?

In the question Function passed as template argument, it was asked and explained that there are two traditional ways of passing a function as a template argument in C++, they're:

  1. Declaring the function as a "functor" (e.g. an object with an overridden () operator). But it requires modifying the target function first.
  2. Using function pointer as the parameter, and taking the address of the function with the & operator to use the template. But this can possibly inhibit compiler optimization, especially inlining, since the compiler may be unable to figure out the target of the naked pointer.

I have read and conceptually understood both methods. However, in a comment by user pfalcon under the answer, it was claimed that:

Fast forward to few years later, situation with using functions as template arguments much improved in C++11. You no longer bound to use Javaisms like functor classes, and can use for example static inline functions as template arguments directly. Still far cry comparing to Lisp macros of 1970s, but C++11 definitely has good progress over the years.

But no expatiation or example is given. What are the supposedly "much improved" ways of doing that in C++11 that pfalcon was referring to? I tried to search "function as template argument C++11" but I didn't see any usable result.

Aucun commentaire:

Enregistrer un commentaire