Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function template as a template parameter

81 views
Skip to first unread message

Edward Diener

unread,
Feb 20, 2011, 10:52:10 AM2/20/11
to

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...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Mathias Gaunard

unread,
Feb 21, 2011, 2:20:46 PM2/21/11
to

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.

Edward Diener

unread,
Feb 22, 2011, 11:58:28 AM2/22/11
to

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" ?

CornedBee

unread,
Feb 24, 2011, 8:18:47 AM2/24/11
to

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

Mathias Gaunard

unread,
Feb 26, 2011, 12:23:12 PM2/26/11
to

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.

0 new messages