Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will C++17 template arguments with auto feature allow constrained std::function objects?

With the upcoming C++17 feature of non-type template arguments with auto, will it be possible to implement std::function in such a way as to be able to put, for example, the following functions:

    bool f(int n,  double d) {}    
    bool g(bool b, char c)   {}
    bool h(bool b)           {}

into auto-templated std::function objects:

   std::function<bool(auto,   auto)> faa = f; // ok
   std::function<bool(int,    auto)> fia = f; // ok
   std::function<bool(double, auto)> fda = f; // error: function type mismatch
   std::function<bool(auto,   auto)> gaa = g; // ok
   std::function<bool(auto,   auto)> haa = h; // error: function type mismatch
   std::function<bool(auto)>         ha  = h; // ok

And so on.

In other words, to have std::function objects constrained on the function types they accept?

(Currently, on GCC we get an error: 'auto' parameter not permitted in this context.)

like image 468
Davit Tevanian Avatar asked Sep 27 '16 14:09

Davit Tevanian


People also ask

What are template arguments enlist types of template arguments?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What is template argument deduction?

Template argument deduction is used when selecting user-defined conversion function template arguments. A is the type that is required as the result of the conversion. P is the return type of the conversion function template.

How do you call a function in a template?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

Which is the correct example of template parameters?

For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.


1 Answers

Those are not non-type template arguments, so auto is not permitted there in C++17.

Non-type template arguments are arguments to templates that are pointers or integers or similar, actual values, not types.

For an example,

std::integral_constant<std::size_t, 7>;

here the 7 is a non-type template argument of type std::size_t and value 7.

Non-type template auto permits something like:

template<auto x>
using integral = std::integral_constant< decltype(x), x >;

now integral<7> is std::integral_constant<int, 7>.

On the other hand, your use of auto is in place of a type, not a non-type.


There is a feature where the type of the template is deduced, so you could write:

std::function faa = f;

if they augmented std::function to be able to deduce a signature from a function pointer (or non-template callable).

Note however that this std::function would have a fixed signature, not a template one. The feature simply allows deduction, not template dynamic dispatch.

I don't know if std::function was augmented in this way in C++17, but the language feature to do so was added.

like image 87
Yakk - Adam Nevraumont Avatar answered Oct 08 '22 19:10

Yakk - Adam Nevraumont