Topic: Template template parameters in C++0x


Author: Edward Diener <eldiener@tropicsoft.invalid>
Date: Wed, 9 Mar 2011 10:21:11 CST
Raw View
On 3/9/2011 9:03 AM, Daniel Kr   gler wrote:
>
> On 2011-03-07 20:06, Edward Diener wrote:
>>
>> On 3/5/2011 8:18 PM, Daniel Kr=FCgler wrote:
>>
>>>
>>> Am 04.03.2011 10:15, schrieb Edward Diener:
>>>
>>>>
>>>> One of the weaknesses of template template parameters in C++03 is that
>>>> they only accept class templates and not function templates to be
>>>> passed as the template argument.
>>>>
>>>> In C++0x, with the addition of template alias, the documentation for a
>>>> template template parameter has changed to allow a class template or a
>>>> template alias to be passed for the template template parameter.
>>>>
>>>> I have a few questions about this which the documentation in 14.3.3
>>>> does not make clear to me:
>>>>
>>>> 1) Can a function template as a template alias be passed as the
>>>> template argument for a template template parameter ?
>>>>
>>>
>>> A function template cannot be passed, but a alias template can be
>>> defined that corresponds to a particular function template type.
>>>
>>
>> I do not see that explicitly defined in the working draft of the C++
>> standard nor in the template alias papers which I have read.
>
> There is no need to mention this explicitly, because this is covered
> by the existing general rules.

Saying that an "alias template can be defined that corresponds to a
particular function template type" because it is "covered by the
existing general rules", without citing where in the C++ standard this
is mentioned, is not answering my question.

>
> Primarily I
>>
>> also did not see any example in the working draft which specified the
>> synta=
>> x
>> for a template alias for a function template. I did see one example in
>> n148=
>> 9
>> in section 2.3 but since that section was written with the proviso that
>> "Consequently, we do not propose the generalizations
>> mentioned in this section" I suppose that it is not considered valid
>> syntax=
>> So are you saying in the above sentence that this is valid:
>>
>> Function temoplate:
>>
>> template<class X, class Y> void Ft(X,Y) {}
>
> OK, this is a function template.
>
>> Template alias:
>>
>> template<class X> using AliasFt = Ft<X,int>(X,int);
>
> This is invalid, because Ft<X,int>(X,int) is not a type, but a
> incompletely specified function template.

Here is an example from n1489:

template<class T>
using Vec = std::vector<T, MyAllocator<T> >;

The syntax 'std::vector<T, MyAllocator<T> >' refers to a class template
type-id.

This is to what I am referring. Given that the above is valid, can a
template alias also refer to a function template type-id ?

>
>> or maybe just:
>>
>> template<class X> using AliasFt = Ft<X,int>;
>
> Also invalid, because Ft<X,int> does not describe a type.

Given that a template alias can refer to a class template where some of
the template parameters are filled in, why do you say that it can not
refer to a function template where some of the parameters are filled in ?

Given a class template:

template<class X,class Y> class ATemplate {};

We can create a template alias:

template<class T>
using CTAlias = ATemplate<T,T>;

or:

template<class T>
using CTAlias = ATemplate<T,int>;

So given a function template:

template<class X,class Y> void AFTemplate(X,Y) { }

is this template alias possible:

template<class T>
using CTFAlias = AFTemplate<T,T>;

or:

template<class T>
using CTFAlias = AFTemplate<T,int>;

or perhaps the correct syntax is:

template<class T>
using CTFAlias = AFTemplate<T,T>(T,T);

or:

template<class T>
using CTFAlias = AFTemplate<T,int>(T,int);

Can you really not see what I mean when I now ask if a template alias
for a function template-id is possible ?



--
[ 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: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Thu, 10 Mar 2011 07:27:01 CST
Raw View
On 2011-03-09 17:21, Edward Diener wrote:
>
> On 3/9/2011 9:03 AM, Daniel Kr   gler wrote:
>>
>> On 2011-03-07 20:06, Edward Diener wrote:
>>>
>>> On 3/5/2011 8:18 PM, Daniel Kr=FCgler wrote:
>>>
>>>>
>>>> Am 04.03.2011 10:15, schrieb Edward Diener:
>>>>
>>>>>
>>>>> One of the weaknesses of template template parameters in C++03 is that
>>>>> they only accept class templates and not function templates to be
>>>>> passed as the template argument.
>>>>>
>>>>> In C++0x, with the addition of template alias, the documentation for a
>>>>> template template parameter has changed to allow a class template or a
>>>>> template alias to be passed for the template template parameter.
>>>>>
>>>>> I have a few questions about this which the documentation in 14.3.3
>>>>> does not make clear to me:
>>>>>
>>>>> 1) Can a function template as a template alias be passed as the
>>>>> template argument for a template template parameter ?
>>>>>
>>>>
>>>> A function template cannot be passed, but a alias template can be
>>>> defined that corresponds to a particular function template type.
>>>>
>>>
>>> I do not see that explicitly defined in the working draft of the C++
>>> standard nor in the template alias papers which I have read.
>>
>> There is no need to mention this explicitly, because this is covered
>> by the existing general rules.
>
> Saying that an "alias template can be defined that corresponds to a
> particular function template type" because it is "covered by the
> existing general rules", without citing where in the C++ standard this
> is mentioned, is not answering my question.

I had expected that the place is obvious, because there is not much
text needed for alias templates. The whole essence can be found in
14.5.7 [temp.alias]. Basically all is said in p.1:

"A template-declaration in which the declaration is an
alias-declaration (Clause 7) declares the identifier to be a alias
template. An alias template is a name for a family of types. The name
of the alias template is a template-name."

The reference to the alias-declaration in Clause 7 brings us to the grammar

alias-declaration:
 using identifier = type-id ;

Now follow the path to type-id and you will notice that describes a
name of a(ny) type.

>>> Function temoplate:
>>>
>>> template<class X, class Y> void Ft(X,Y) {}
>>
>> OK, this is a function template.
>>
>>> Template alias:
>>>
>>> template<class X> using AliasFt = Ft<X,int>(X,int);
>>
>> This is invalid, because Ft<X,int>(X,int) is not a type, but a
>> incompletely specified function template.
>
> Here is an example from n1489:
>
> template<class T>
> using Vec = std::vector<T, MyAllocator<T> >;
>
> The syntax 'std::vector<T, MyAllocator<T> >' refers to a class template
> type-id.

Exactly, the same conclusion can be drawn from the grammar shown above.

> This is to what I am referring. Given that the above is valid, can a
> template alias also refer to a function template type-id ?

Any type-id can be associated with an alias possibly except for some
restrictions to linkage. I demonstrated how to associate a function
type with an alias template in my first reply, e.g.

template <class R, class T>
using F = R(T);

is such a form. Thus F<void, int> is an alias to the function type
void(int). But my understanding is that you do not want this, you want
to alias a function (template)s, but not a function (template)
type-id.

>>> template<class X> using AliasFt = Ft<X,int>;
>>
>> Also invalid, because Ft<X,int> does not describe a type.
>
> Given that a template alias can refer to a class template where some of
> the template parameters are filled in, why do you say that it can not
> refer to a function template where some of the parameters are filled in ?
>
> Given a class template:
>
> template<class X,class Y> class ATemplate {};
>
> We can create a template alias:
>
> template<class T>
> using CTAlias = ATemplate<T,T>;
>
> or:
>
> template<class T>
> using CTAlias = ATemplate<T,int>;

Yes.

> So given a function template:
>
> template<class X,class Y> void AFTemplate(X,Y) { }
>
> is this template alias possible:
>
> template<class T>
> using CTFAlias = AFTemplate<T,T>;
>
> or:
>
> template<class T>
> using CTFAlias = AFTemplate<T,int>;
>
> or perhaps the correct syntax is:
>
> template<class T>
> using CTFAlias = AFTemplate<T,T>(T,T);
>
> or:
>
> template<class T>
> using CTFAlias = AFTemplate<T,int>(T,int);
>
> Can you really not see what I mean when I now ask if a template alias
> for a function template-id is possible ?

I only see that you are not trying to form type-id's.

"AFTemplate<T,int>(T,int)" is not a type-id, it is an (incomplete)
specialization of the function template AFTemplate.

HTH & Greetings from Bremen,

Daniel Kr   gler


--
[ 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: Thu, 10 Mar 2011 10:24:39 CST
Raw View
[ Moderator's note: Over-quoted. Please quote only the parts of a
  message that provide the necessary context. -sdc ]

On 3/10/2011 8:27 AM, Daniel Kr   gler wrote:
>
> On 2011-03-09 17:21, Edward Diener wrote:
>>
>> On 3/9/2011 9:03 AM, Daniel Kr   gler wrote:
>>>
>>> On 2011-03-07 20:06, Edward Diener wrote:
>>>>
>>>> On 3/5/2011 8:18 PM, Daniel Kr=FCgler wrote:
>>>>
>>>>>
>>>>> Am 04.03.2011 10:15, schrieb Edward Diener:
>>>>>
>>>>>>
>>>>>> One of the weaknesses of template template parameters in C++03 is
>>>>>> that
>>>>>> they only accept class templates and not function templates to be
>>>>>> passed as the template argument.
>>>>>>
>>>>>> In C++0x, with the addition of template alias, the documentation
>>>>>> for a
>>>>>> template template parameter has changed to allow a class template
>>>>>> or a
>>>>>> template alias to be passed for the template template parameter.
>>>>>>
>>>>>> I have a few questions about this which the documentation in 14.3.3
>>>>>> does not make clear to me:
>>>>>>
>>>>>> 1) Can a function template as a template alias be passed as the
>>>>>> template argument for a template template parameter ?
>>>>>>
>>>>>
>>>>> A function template cannot be passed, but a alias template can be
>>>>> defined that corresponds to a particular function template type.
>>>>>
>>>>
>>>> I do not see that explicitly defined in the working draft of the C++
>>>> standard nor in the template alias papers which I have read.
>>>
>>> There is no need to mention this explicitly, because this is covered
>>> by the existing general rules.
>>
>> Saying that an "alias template can be defined that corresponds to a
>> particular function template type" because it is "covered by the
>> existing general rules", without citing where in the C++ standard this
>> is mentioned, is not answering my question.
>
> I had expected that the place is obvious, because there is not much
> text needed for alias templates. The whole essence can be found in
> 14.5.7 [temp.alias]. Basically all is said in p.1:
>
> "A template-declaration in which the declaration is an
> alias-declaration (Clause 7) declares the identifier to be a alias
> template. An alias template is a name for a family of types. The name
> of the alias template is a template-name."
>
> The reference to the alias-declaration in Clause 7 brings us to the grammar
>
> alias-declaration:
> using identifier = type-id ;
>
> Now follow the path to type-id and you will notice that describes a
> name of a(ny) type.

See my comments about type-id at the end.

>
>>>> Function temoplate:
>>>>
>>>> template<class X, class Y> void Ft(X,Y) {}
>>>
>>> OK, this is a function template.
>>>
>>>> Template alias:
>>>>
>>>> template<class X> using AliasFt = Ft<X,int>(X,int);
>>>
>>> This is invalid, because Ft<X,int>(X,int) is not a type, but a
>>> incompletely specified function template.
>>
>> Here is an example from n1489:
>>
>> template<class T>
>> using Vec = std::vector<T, MyAllocator<T> >;
>>
>> The syntax 'std::vector<T, MyAllocator<T> >' refers to a class template
>> type-id.
>
> Exactly, the same conclusion can be drawn from the grammar shown above.
>
>> This is to what I am referring. Given that the above is valid, can a
>> template alias also refer to a function template type-id ?
>
> Any type-id can be associated with an alias possibly except for some
> restrictions to linkage. I demonstrated how to associate a function
> type with an alias template in my first reply, e.g.
>
> template <class R, class T>
> using F = R(T);

R(T) is a type-id for a function type, not a type-id for a function
template type.

>
> is such a form. Thus F<void, int> is an alias to the function type
> void(int). But my understanding is that you do not want this, you want
> to alias a function (template)s, but not a function (template)
> type-id.

My understanding is that a type-id for a function type is different from
a type-id of a function template.

Somehow you are saying that a type-id can refer to a class template but
not a function template. Again see my comments at the end.

>
>>>> template<class X> using AliasFt = Ft<X,int>;
>>>
>>> Also invalid, because Ft<X,int> does not describe a type.
>>
>> Given that a template alias can refer to a class template where some of
>> the template parameters are filled in, why do you say that it can not
>> refer to a function template where some of the parameters are filled in ?
>>
>> Given a class template:
>>
>> template<class X,class Y> class ATemplate {};
>>
>> We can create a template alias:
>>
>> template<class T>
>> using CTAlias = ATemplate<T,T>;
>>
>> or:
>>
>> template<class T>
>> using CTAlias = ATemplate<T,int>;
>
> Yes.
>
>> So given a function template:
>>
>> template<class X,class Y> void AFTemplate(X,Y) { }
>>
>> is this template alias possible:
>>
>> template<class T>
>> using CTFAlias = AFTemplate<T,T>;
>>
>> or:
>>
>> template<class T>
>> using CTFAlias = AFTemplate<T,int>;
>>
>> or perhaps the correct syntax is:
>>
>> template<class T>
>> using CTFAlias = AFTemplate<T,T>(T,T);
>>
>> or:
>>
>> template<class T>
>> using CTFAlias = AFTemplate<T,int>(T,int);
>>
>> Can you really not see what I mean when I now ask if a template alias
>> for a function template-id is possible ?
>
> I only see that you are not trying to form type-id's.
>
> "AFTemplate<T,int>(T,int)" is not a type-id, it is an (incomplete)
> specialization of the function template AFTemplate.

I do not understand why, given:

Class template: template<class X,class Y> class ATemplate {};

Template alias: template<class T> using CTAlias = ATemplate<T,int>;

you are saying that ATemplate<T,int> is a type-id

but:

Function template: template<class X,class Y> void AFTemplate(X,Y) { }

Template alias: template<class T> using CTFAlias = AFTemplate<T,int>
(T,int);

you are saying that AFTemplate<T,int>(T,int) ) is not a type-id ?

There is really something I am missing in this analogy. Furthermore you
see to think that the form:

ATemplate<T,int>

represents a complete specialization while the form:

AFTemplate<T,int> (T,int)

represents an incomplete specialization.

My understanding of a template alias ( which you call "alias template"
for some reason ) is that the from of the "type-id" portion of the
syntax may have one or more template parameters specified, and has
nothing to do with complete or incomplete specialization.


--
[ 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: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sat, 12 Mar 2011 09:27:08 CST
Raw View
[Second try after 24 hours]

Am 10.03.2011 17:24, schrieb Edward Diener:
Daniel wrote here:
>> Any type-id can be associated with an alias possibly except for some
>> restrictions to linkage. I demonstrated how to associate a function
>> type with an alias template in my first reply, e.g.
>>
>> template <class R, class T>
>> using F = R(T);
>
> R(T) is a type-id for a function type, not a type-id for a function
> template type.

Sure, because there does not exist a concept of a type-id for a
function template type. If you can find any way of describing a
concrete function template signature as a type, you can express this
via an alias template.

Looking from a different angle: Each concrete specialization of a
function template has a given function type. Alias templates allow me
to define the family of function types that I could form given a
function template or given a normal function. But there is not way to
describe the type of a function template itself.

>> is such a form. Thus F<void, int> is an alias to the function type
>> void(int). But my understanding is that you do not want this, you want
>> to alias a function (template)s, but not a function (template)
>> type-id.
>
> My understanding is that a type-id for a function type is different from
> a type-id of a function template.
>
> Somehow you are saying that a type-id can refer to a class template but
> not a function template.

Correct, because a function template does not refer to a type.

[..]

>>> Can you really not see what I mean when I now ask if a template alias
>>> for a function template-id is possible ?
>>
>> I only see that you are not trying to form type-id's.
>>
>> "AFTemplate<T,int>(T,int)" is not a type-id, it is an (incomplete)
>> specialization of the function template AFTemplate.
>
> I do not understand why, given:
>
> Class template: template<class X,class Y> class ATemplate {};
>
> Template alias: template<class T> using CTAlias = ATemplate<T,int>;
>
> you are saying that ATemplate<T,int> is a type-id

Yes, because ATemplate<T,int> is a type for every T that is a type
itself. T must be a type, because the template declaration of
CTAlias says that it has a type parameter. Of-course we could just
follow the grammar of /type-id/ and could confirm that
ATemplate<T,int> satisfies the criteria for a type-id.

> but:
>
> Function template: template<class X,class Y> void AFTemplate(X,Y) { }

Note the difference: A class template describes a family of classes,
and each class is a type. An alias template describes a family of
types. But a function template describes a family of *functions*,
*not*
a family of function types and also not a "function template type".

> Template alias: template<class T> using CTFAlias = AFTemplate<T,int>
> (T,int);

This is ill-formed, because AFTemplate<T,int> is never a type-id, it
just names a specialization of the function template AFTemplate, which
is not a type (but any such specialization *has* a function type).

Reduce this even more by removing all open template parameters: You
need to ask whether

AFTemplate<void,int>

describes a type. Does it? No, it doesn't, it describes a *function*
and this function has some function type. But there does not exist a
concept of a function template type. If you don't believe me then wait
for the first compiler that supports non-template alias declarations.
If your assumption where correct, you could write

template<class T> void blubb();

using f_t = blubb<int>;

But this won't work, of-course, because blubb<int> does not describe a type.

> you are saying that AFTemplate<T,int>(T,int) ) is not a type-id ?

Exactly.

> There is really something I am missing in this analogy. Furthermore you
> see to think that the form:
>
> ATemplate<T,int>
>
> represents a complete specialization while the form:
>
> AFTemplate<T,int> (T,int)
>
> represents an incomplete specialization.

I don't know where you got the impression, that I thought that
ATemplate<T,int> represents a complete specialization. Assuming that T
is a template parameter, ATemplate<T,int> still refers to a family of
class types, while AFTemplate<T,int> (T,int) still refers to a family
of functions.

> My understanding of a template alias ( which you call "alias template"
> for some reason )

The reason for naming it this way is because the standard C++ committee
is going to bring this name in symmetry to other template forms (class
template, function template). This is a consequence of resolving
national body comment FI 11:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3249.html#FI11

You may notice that the most recent working draft n3242 has already
implemented this change.

> is that the from of the "type-id" portion of the
> syntax may have one or more template parameters specified, and has
> nothing to do with complete or incomplete specialization.

I'm not saying that it has to do with complete or incomplete
specialization, but an alias template is still a template and thus has
template parameters. If these template parameters are used in the
definition of the alias declaration, the referred to type is not yet
completely known. And an alias template *always* - like any other
alias declaration or typedef declaration - refers to types, not to
functions.

HTH & Greetings from Bremen,

Daniel Kr   gler


--
[ 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: "Johannes Schaub (litb)" <schaub.johannes@googlemail.com>
Date: Sun, 13 Mar 2011 09:55:17 CST
Raw View
Edward Diener wrote:

>
> One of the weaknesses of template template parameters in C++03 is that
> they only accept class templates and not function templates to be
> passed as the template argument.
>
> In C++0x, with the addition of template alias, the documentation for a
> template template parameter has changed to allow a class template or a
> template alias to be passed for the template template parameter.
>
> I have a few questions about this which the documentation in 14.3.3
> does not make clear to me:
>
> 1) Can a function template as a template alias be passed as the
> template argument for a template template parameter ?
>

As Daniel explained you cannot pass the function template directly. However
you can work around that.

     template<typename T, T F>
     struct ct_value {
       typedef T type;

       operator T() const {
         return F;
       }
     };

With this in place, you can use it as follows:

     template<class X, class Y> void Ft(X,Y) {}
     template<class X>
     using FtSpec = ct_value<void(X, int), Ft<X, int>>;

     // wishes to receive templated function object
     template<template<typename X> class Template>
     void f() {
       Template<float>()(1.0f, 10);
     }

     int main() {
       f<FtSpec>();
     }

With the C++0x rules, this works for function templates with both internal
and external linkage. Perhaps it's easier to use when wrapped in a macro:

     #define PASSABLE(Name, Template) \
     template<typename ...T> \
     using Name = ct_value<decltype(& Template< T... >), & Template< T... >>

     PASSABLE(FtSpec, Ft);

Hope this helps as a workaround.


--
[ 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: Thu, 17 Mar 2011 12:20:34 CST
Raw View
On 3/12/2011 10:27 AM, Daniel Kr=FCgler wrote:

> I do not understand why, given:
>>
>> Class template: template<class X,class Y>  class ATemplate {};
>>
>> Template alias: template<class T>  using CTAlias = ATemplate<T,int>;
>>
>> you are saying that ATemplate<T,int>  is a type-id
>>
>
> Yes, because ATemplate<T,int>  is a type for every T that is a type
> itself. T must be a type, because the template declaration of
> CTAlias says that it has a type parameter. Of-course we could just
> follow the grammar of /type-id/ and could confirm that
> ATemplate<T,int>  satisfies the criteria for a type-id.
>
> but:
>>
>> Function template: template<class X,class Y>  void AFTemplate(X,Y) { }
>>
>
> Note the difference: A class template describes a family of classes,
> and each class is a type. An alias template describes a family of
> types. But a function template describes a family of *functions*,
> *not*
> a family of function types and also not a "function template type".
>
>
I am going to focus on what you say here, because it is the crux of why I d=
o
not understand your explanation.

I do not understand how you can say that a function template describes a
family of functions but that each function in that family of functions is
not a type.

Class template: template<class X> class ClassTemplate { };

for each instantiation of ClassTemplate we have a different type:

ClassTemplate<int> is a type.
ClassTemplate<short> is another type.
etc. etc.

Function template: template<class X> void FunctionTemplate (X)  { }

for each instantiation of FunctionTemplate we have a different type:

FunctionTemplate<int> is the type 'void (int)'
FunctionTemplate<short> is the type 'void (short)'
etc. etc.

Surely a function instantiated from a function template is a type just as a
class instantiated from a class template is a type.

Regarding type-id, in section 8.1 of the working standard I read:

"To specify type conversions explicitly, and as an argument of sizeof,
alignof, new, or typeid, the name of
a type shall be specified. This can be done with a type-id, which is
syntactically a declaration for a variable
or function of that type that omits the name of the entity."

It appears to me, from the above, that a type-id can refer to a function
type.

Now you may be arguing that for the purposes of an alias template ( I will
use your terminology since I also say 'class template' rather than 'templat=
e
class' and 'function template' rather than 'template function' ) the
'type-id' part can refer to a class template and not a function template.
That is fine although I do not think it is spelled out in the C++ standard
specification. But that at least answers the  initial question of my OP.


--
[ 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: usenet@mkarcher.dialup.fu-berlin.de (Michael Karcher)
Date: Fri, 18 Mar 2011 12:15:21 CST
Raw View
Edward Diener <eldiener@tropicsoft.invalid> wrote:
> I do not understand how you can say that a function template describes a
> family of functions but that each function in that family of functions is
> not a type.
>
> Class template: template<class X> class ClassTemplate { };
>
> for each instantiation of ClassTemplate we have a different type:
>
> ClassTemplate<int> is a type.
> ClassTemplate<short> is another type.

You are right.

> Function template: template<class X> void FunctionTemplate (X)  { }
>
> for each instantiation of FunctionTemplate we have a different type:
>
> FunctionTemplate<int> is the type 'void (int)'
> FunctionTemplate<short> is the type 'void (short)'

No, FunctionTemplate<int> is *not* the type 'void (int)', probably better
spelled as 'void ()(int)', but it is a *function* having that type.

> Surely a function instantiated from a function template is a type just as a
> class instantiated from a class template is a type.
If something is a type, I can write "<type>* x" to declare a variable
pointing to an object having that type.

Surely this is will work: "ClassTemplate<int> * x;"
And this one is a syntax error: "FunctionTemplate<int> * x;"

> It appears to me, from the above, that a type-id can refer to a function
> type.

Yes. "void ()(int)" is a type-id. This type-ID describes functions returning
void and accepting one argument of type int. And of course you can declare a
pointer to such functions: "void (*x)(int)".

> Now you may be arguing that for the purposes of an alias template ( I will
> use your terminology since I also say 'class template' rather than 'template
> class' and 'function template' rather than 'template function' )
A class template is a template that can be used to instanciate classes.
A template class is one instance of this class template.

> the 'type-id' part can refer to a class template
No, a class template is not a type, but a template used to generate types by
instantiating the class template to obtain template classes. These template
classes are types, so they can be used as type-ids.

> and not a function template.
Also, along the same lines, a function template is not a type. A function
template also is not an object, but just a template that can be
instantiated. After instantiating a function template, you obtain a template
function, which is a special kind of functions. As functions are not types,
also template functions can not be used as type-ids.

Regards,
 Michael Karcher


--
[ 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: Richard Smith <richard@ex-parrot.com>
Date: Sat, 19 Mar 2011 13:20:39 CST
Raw View
On Mar 18, 6:15 pm, use...@mkarcher.dialup.fu-berlin.de (Michael
Karcher) wrote:

> No, FunctionTemplate<int> is *not* the type 'void (int)', probably better
> spelled as 'void ()(int)', but it is a *function* having that type.

'void (int)' is certainly not better spelled as 'void ()(int)'.  The
former is legal; the latter is not.  If you don't believe me, try to
compile:

 template <class T> struct X {};
 typedef X< void ()(int) > Y;

It doesn't work.  However your point is valid.

 FunctionTemplate<int> is not a type, it is a function which *has* a
type.

In summary:

 template <class T> class C {};

'C<int>' is a type.

 template <class T> using CA = C<T>;

'CA<int>' is a type: it is the type 'C<int>'.

 template <class T> void F(T t) {}

'F<int>' is a function; it is *not* a type, but it *has* a type, 'void
(int)'.

 template <class T> using FA = void (T t);

'FA<int>' is a type: it is the type 'void (int)'.

--
Richard Smith


--
[ 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: Fri, 4 Mar 2011 03:15:33 CST
Raw View
One of the weaknesses of template template parameters in C++03 is that
they only accept class templates and not function templates to be
passed as the template argument.

In C++0x, with the addition of template alias, the documentation for a
template template parameter has changed to allow a class template or a
template alias to be passed for the template template parameter.

I have a few questions about this which the documentation in 14.3.3
does not make clear to me:

1) Can a function template as a template alias be passed as the
template argument for a template template parameter ?

2) if it can:

a) How are the call parameters for a function template resolved
against the template template parameter, since the syntax of the
template template parameter involves only template parameters ?

b) Why does not C++0x allow a function template to be passed directly
as an argument for a template template parameter ?

Of course if a function template as a template alias can not be passed
as the template argument for a template template parameter in C++0x,
a) and b) can be disregarded.


--
[ 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: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sat, 5 Mar 2011 19:18:10 CST
Raw View
Am 04.03.2011 10:15, schrieb Edward Diener:
>
> One of the weaknesses of template template parameters in C++03 is that
> they only accept class templates and not function templates to be
> passed as the template argument.
>
> In C++0x, with the addition of template alias, the documentation for a
> template template parameter has changed to allow a class template or a
> template alias to be passed for the template template parameter.
>
> I have a few questions about this which the documentation in 14.3.3
> does not make clear to me:
>
> 1) Can a function template as a template alias be passed as the
> template argument for a template template parameter ?

A function template cannot be passed, but a alias template can be
defined that corresponds to a particular function template type. This
was a misunderstanding of several of us (including mine) because of
your comparison with template template parameters which allow to
provide a "template type" definition to a template, but not a
"template value", so to say. Alias templates similarly allow for
defining "template types", not values.

I'm not aware of any proposal that suggests this extension of
templates. At this point I don't see fundamental technical problems
with such an approach, but I'm not a compiler designer so you might
try to contact one (if you can).

Could you explain us a bit what particular use-case you have in your
mind that can be solved via these entities but not with existing
language constructions? It would be a good starting point for a
proposal that you could submit to the C++ Standards Committee.

HTH & Greetings from Bremen,

Daniel Kr   gler




--
[ 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: Mon, 7 Mar 2011 13:06:03 CST
Raw View
On 3/5/2011 8:18 PM, Daniel Kr=FCgler wrote:

>
> Am 04.03.2011 10:15, schrieb Edward Diener:
>
>>
>> One of the weaknesses of template template parameters in C++03 is that
>> they only accept class templates and not function templates to be
>> passed as the template argument.
>>
>> In C++0x, with the addition of template alias, the documentation for a
>> template template parameter has changed to allow a class template or a
>> template alias to be passed for the template template parameter.
>>
>> I have a few questions about this which the documentation in 14.3.3
>> does not make clear to me:
>>
>> 1) Can a function template as a template alias be passed as the
>> template argument for a template template parameter ?
>>
>
> A function template cannot be passed, but a alias template can be
> defined that corresponds to a particular function template type.
>

I do not see that explicitly defined in the working draft of the C++
standard nor in the template alias papers which I have read. Primarily I
also did not see any example in the working draft which specified the synta=
x
for a template alias for a function template. I did see one example in n148=
9
in section 2.3 but since that section was written with the proviso that
"Consequently, we do not propose the generalizations
mentioned in this section" I suppose that it is not considered valid syntax=
So are you saying in the above sentence that this is valid:

Function temoplate:

template<class X, class Y> void Ft(X,Y) {}

Template alias:

template<class X> using AliasFt = Ft<X,int>(X,int);

or maybe just:

template<class X> using AliasFt = Ft<X,int>;

This
> was a misunderstanding of several of us (including mine) because of
> your comparison with template template parameters which allow to
> provide a "template type" definition to a template, but not a
> "template value", so to say. Alias templates similarly allow for
> defining "template types", not values.
>

I do not know what you mean by the distinction between a "template type" an=
d
a "template value". If I have a C++03 template template parameter, I pass i=
t
as an argument an actual class template. Similarly, according to what I
discern from the current working standard, if I have a C++0x template
template parameter I pass it as an argument an actual class template or a
template alias. My reading of template alias is that it is just another nam=
e
for a template with possibly some ( or all ) of the template arguments
specified. My question is simply whether the "type expression" part of a
template alias, as given in n1489 section 1.2, can refer to a function
template ?


> I'm not aware of any proposal that suggests this extension of
> templates. At this point I don't see fundamental technical problems
> with such an approach, but I'm not a compiler designer so you might
> try to contact one (if you can).
>
> Could you explain us a bit what particular use-case you have in your
> mind that can be solved via these entities but not with existing
> language constructions? It would be a good starting point for a
> proposal that you could submit to the C++ Standards Committee.
>

The use case is quite simply that currently a template template parameter
allows a class template to be passed as an argument but not a function
template. Are you asking what use case I have for allowing a function
template to be passed for a template template argument ?


--
[ 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: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 9 Mar 2011 08:03:14 CST
Raw View
On 2011-03-07 20:06, Edward Diener wrote:
>
> On 3/5/2011 8:18 PM, Daniel Kr=FCgler wrote:
>
>>
>> Am 04.03.2011 10:15, schrieb Edward Diener:
>>
>>>
>>> One of the weaknesses of template template parameters in C++03 is that
>>> they only accept class templates and not function templates to be
>>> passed as the template argument.
>>>
>>> In C++0x, with the addition of template alias, the documentation for a
>>> template template parameter has changed to allow a class template or a
>>> template alias to be passed for the template template parameter.
>>>
>>> I have a few questions about this which the documentation in 14.3.3
>>> does not make clear to me:
>>>
>>> 1) Can a function template as a template alias be passed as the
>>> template argument for a template template parameter ?
>>>
>>
>> A function template cannot be passed, but a alias template can be
>> defined that corresponds to a particular function template type.
>>
>
> I do not see that explicitly defined in the working draft of the C++
> standard nor in the template alias papers which I have read.

There is no need to mention this explicitly, because this is covered
by the existing general rules.

Primarily I
>
> also did not see any example in the working draft which specified the
> synta=
> x
> for a template alias for a function template. I did see one example in
> n148=
> 9
> in section 2.3 but since that section was written with the proviso that
> "Consequently, we do not propose the generalizations
> mentioned in this section" I suppose that it is not considered valid
> syntax=
> So are you saying in the above sentence that this is valid:
>
> Function temoplate:
>
> template<class X, class Y> void Ft(X,Y) {}

OK, this is a function template.

> Template alias:
>
> template<class X> using AliasFt = Ft<X,int>(X,int);

This is invalid, because Ft<X,int>(X,int) is not a type, but a
incompletely specified function template.

> or maybe just:
>
> template<class X> using AliasFt = Ft<X,int>;

Also invalid, because Ft<X,int> does not describe a type.

> This
>>
>> was a misunderstanding of several of us (including mine) because of
>> your comparison with template template parameters which allow to
>> provide a "template type" definition to a template, but not a
>> "template value", so to say. Alias templates similarly allow for
>> defining "template types", not values.
>>
>
> I do not know what you mean by the distinction between a "template type"
> an=
> d
> a "template value".

I tried to explain in in different words above. What I demonstrated in
my original reply was showing how to define an alias template for a
function type family. Your attempts above don't work, because they are
not referring to types. This is similar to the attempt tom define
a typedef for an object named x:

int x;

typedef x y;

Again, this was the reason of the misunderstanding of most
participants of the thread.

If I have a C++03 template template parameter, I
>
> pass i=
> t
> as an argument an actual class template.

Yes, but this is like providing a type, even though at this point of
providing the type, the final specialization is not yet fixed.

> Similarly, according to what I
> discern from the current working standard, if I have a C++0x template
> template parameter I pass it as an argument an actual class template or a
> template alias. My reading of template alias is that it is just another
> nam=
> e
> for a template with possibly some ( or all ) of the template arguments
> specified.

This is incorrect. You can define an alias template to refer to
non-template types. Possible examples are:

template<class T>
using A = T*;

or

template<class T>
using B = int;

This allows to extend the capabilities of template-template parameters
to refer to more than just class templates, but is still restricted to
types ("an alias for a family of types" as the standard says).

> My question is simply whether the "type expression" part of a
>
> template alias, as given in n1489 section 1.2, can refer to a function
> template ?

It can refer to a function type family or some particular function
type, but not to a function, which would would be more a form of a
non-type template. There is nothing in the language that currently
allows for "template non-type" template parameters and you are asking
for it. I don't say that it does not work, but no-one has yet
suggested it, AFAIK. I think that Francis Glassborow was the one, who
understood your question from begin with.

> The use case is quite simply that currently a template template parameter
> allows a class template to be passed as an argument but not a function
> template. Are you asking what use case I have for allowing a function
> template to be passed for a template template argument ?

Yes, I'm asking for exactly this. Note that I understand, that it
would be *possible* to allow this, but defining the complete
specification and demonstrating that it is implementable is a bunch of
work. I think it is legitimate to ask for more reasons than "because
it should be possible, so make it possible". Good motivating examples
are usually the foundation of a proposal that suggests a new language
feature.

HTH & Greetings from Bremen,

Daniel Kr   gler



--
[ 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                      ]