When i am trying to pass a function as a template template type parameter to a class, error encountered. Why language core developers won't enable this ability? Functor class templates could be passed, but function templates can't.
For example, compiling this code in "g++ (Ubuntu 8.3.0-6ubuntu1) 8.3.0":
#include <iostream>
using namespace std;
template <template <typename> typename Functor>
class UseFunc
{
public:
void use()
{
Functor<int>(3);
Functor<char>('s');
}
};
template <typename T>
void func(T s)
{
cout << s << endl;
}
int main()
{
UseFunc<func> u {};
u.use();
}
tells:
kek.cpp: In function ‘int main()’:
kek.cpp:24:14: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class> class Functor> class UseFunc’
UseFunc<func> u {};
^
kek.cpp:24:14: note: expected a class template, got ‘func’
kek.cpp:25:4: error: request for member ‘use’ in ‘u’, which is of non-class type ‘int’
u.use();
^~~
I expected it to be implemented the same way as template template parameter passing is. At the end of the day it is just a request to compiler, so that functions of concrete types could be instantiated and used in a usual way.
Truly don't understand this limitation and it would be great if someone could tell me what is the difficulty of implementing this.
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 can be passed by non-type template parameters during compile time? Explanation: Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.
8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
It is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.
I don't know for sure the answer to the question of why C++ does not provide function template template parameters, but I imagine it has something to do with:
auto
but this is obviously way too late for C++98 to have function template template parameters)If you believe that you know how to address all these concerns, and in particular can provide a compelling argument for why we need this feature in the language despite the fact that it would be complicated and we can work around its absence using class template template parameters, feel free to write up a proposal for the standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With