Topic: Function template as a template parameter


Author: Edward Diener <eldiener@tropicsoft.invalid>
Date: Sun, 20 Feb 2011 09:52:10 CST
Raw View
Is there any historic or technical reason why C++ allows a template
parameter to be a class template but C++ does not allow a template
parameter to be a function template ?

Argument:

Templates are a mechanism for type creation. A class template creates
what we may think of as a data type through the instantiation of a class
template. A function template creates what we may think of as a function
type through the instantiation of a function template.

Yet while we can pass class templates to both class templates and
function templates as a template parameter, we can not pass function
templates to either class templates or function templates as a template
parameter. This seems like an anomaly of the C++ template specification.
So I am curious in knowing why this anomaly exists, and whether this has
ever been discussed by the C++ standard committee in any way.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Mon, 21 Feb 2011 13:20:46 CST
Raw View
On Feb 20, 4:52 pm, Edward Diener <eldie...@tropicsoft.invalid> wrote:
> Is there any historic or technical reason why C++ allows a template
> parameter to be a class template but C++ does not allow a template
> parameter to be a function template ?

Function pointers are possible as template arguments because they're
symbols, template functions are not.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Edward Diener <eldiener@tropicsoft.invalid>
Date: Tue, 22 Feb 2011 10:58:28 CST
Raw View
On 2/21/2011 2:20 PM, Mathias Gaunard wrote:
>
> On Feb 20, 4:52 pm, Edward Diener<eldie...@tropicsoft.invalid>  wrote:
>>
>> Is there any historic or technical reason why C++ allows a template
>> parameter to be a class template but C++ does not allow a template
>> parameter to be a function template ?
>
> Function pointers are possible as template arguments because they're
> symbols, template functions are not.

What do you mean by "symbol" ?

A function pointer can be a non-type template argument. What has that
do with a "symbol" ?

Why is a class template not a "symbol" but a function template is a "symbol" ?




--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Thu, 24 Feb 2011 07:18:47 CST
Raw View
On Feb 20, 4:52 pm, Edward Diener <eldie...@tropicsoft.invalid> wrote:
> Is there any historic or technical reason why C++ allows a template
> parameter to be a class template but C++ does not allow a template
> parameter to be a function template ?
>
> Argument:
>
> Templates are a mechanism for type creation. A class template creates
> what we may think of as a data type through the instantiation of a class
> template. A function template creates what we may think of as a function
> type through the instantiation of a function template.

No. Instantiation of a function template yields a function, not a
function type. Now, that function has a type, but that's a minor part
of the issue. And in C++0x, you could actually pass a template alias
that resolves to a function type if you want that:

template <typename Arg>
using FunctionType = void (Arg);

template <template <typename> class FnTypeGenerator>
class foo {
 FnTypeGenerator<int> *intFunctionPointer;
 FnTypeGenerator<float> *floatFunctionPointer;
};

>
> Yet while we can pass class templates to both class templates and
> function templates as a template parameter, we can not pass function
> templates to either class templates or function templates as a template
> parameter. This seems like an anomaly of the C++ template specification.
> So I am curious in knowing why this anomaly exists, and whether this has
> ever been discussed by the C++ standard committee in any way.

There are no non-type template parameters of function types, and there
are no non-type template template parameters at all. Therefore, you
cannot pass function templates as arguments to templates.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Sat, 26 Feb 2011 11:23:12 CST
Raw View
On Feb 22, 5:58 pm, Edward Diener <eldie...@tropicsoft.invalid> wrote:
> On 2/21/2011 2:20 PM, Mathias Gaunard wrote:
>
>
>
> > On Feb 20, 4:52 pm, Edward Diener<eldie...@tropicsoft.invalid>  wrote:
>
> >> Is there any historic or technical reason why C++ allows a template
> >> parameter to be a class template but C++ does not allow a template
> >> parameter to be a function template ?
>
> > Function pointers are possible as template arguments because they're
> > symbols, template functions are not.
>
> What do you mean by "symbol" ?
>
> A function pointer can be a non-type template argument. What has that
> do with a "symbol" ?
>
> Why is a class template not a "symbol" but a function template is a "symbol" ?

Because they have linkage.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]