Topic: why typename and not just type
Author: "Joachim Faulhaber" <afojgo@googlemail.com>
Date: Tue, 3 Apr 2007 15:22:01 CST Raw View
On 1 Apr., 21:52, p...@versatilecoding.com (Pete Becker) wrote:
>
> template <class T1, class T1, class R> works for me. (Aside from the
> obvious typo)
>
for me
template <class T1, class T1, class R> ...
is not really sound if I write a template that is intended to work not
only with class types but also with fundamental and other types. I
know that this does work with the current standard but the wording is
obviously misleading.
With concepts you ought to be more precise in the future
template <LessThanComparable T> ...
T is a type that conforms the signatures (and axioms) of concept
LessThanComparable. But with keyword class I can be rather imprecise.
template <class T> ...
T is a type that can be a class AND ANY OTHER TYPE.
Due to the experience with generic programming and the broader view
that concepts open up, we recognize that most of the time we work with
general types and not just classtype abstractions. Due to this
imprecise wording is seems that
<class T>
parameter declarations are very much out of fashion. For instance in
Gregor & Stroustrups Concept paper [N2081] there are dozens of
<typename T> parameter declarations but only two <class T> parameters.
So you can either be lengthy
template <typename T1, typename T1, typename R> ...
or imprecise
template <class T1, class T1, class R> ...
template <type T1, type T1, type R> ...
would be precise and short.
-- Joachim
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: pete@versatilecoding.com (Pete Becker)
Date: Tue, 3 Apr 2007 22:10:20 GMT Raw View
Joachim Faulhaber wrote:
> On 1 Apr., 21:52, p...@versatilecoding.com (Pete Becker) wrote:
>> template <class T1, class T1, class R> works for me. (Aside from the
>> obvious typo)
>>
> for me
>
> template <class T1, class T1, class R> ...
>
> is not really sound if I write a template that is intended to work not
> only with class types but also with fundamental and other types. I
> know that this does work with the current standard but the wording is
> obviously misleading.
>
It works with the current standard, past standards, and it will work
with all future standards. It's not misleading unless you think that
"class" here means "class and only class," which it doesn't. I have yet
to hear from anyone other than an absolute beginner who was confused by
this.
> With concepts you ought to be more precise in the future
>
> template <LessThanComparable T> ...
>
> T is a type that conforms the signatures (and axioms) of concept
> LessThanComparable. But with keyword class I can be rather imprecise.
>
> template <class T> ...
>
> T is a type that can be a class AND ANY OTHER TYPE.
>
Yes, that's exactly what it means. What's the problem?
> Due to the experience with generic programming and the broader view
> that concepts open up, we recognize that most of the time we work with
> general types and not just classtype abstractions. Due to this
> imprecise wording is seems that
>
> <class T>
>
> parameter declarations are very much out of fashion.
Oh, I know they're out of fashion. But fashion is not based on technical
merit.
> For instance in
> Gregor & Stroustrups Concept paper [N2081] there are dozens of
> <typename T> parameter declarations but only two <class T> parameters.
>
> So you can either be lengthy
> template <typename T1, typename T1, typename R> ...
> or imprecise
> template <class T1, class T1, class R> ...
>
> template <type T1, type T1, type R> ...
> would be precise and short.
>
Sorry, but adding "type" as a keyword would break an awful lot of code.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: dave@boost-consulting.com (David Abrahams)
Date: Wed, 4 Apr 2007 13:45:47 GMT Raw View
on Tue Apr 03 2007, pete-AT-versatilecoding.com (Pete Becker) wrote:
> Joachim Faulhaber wrote:
>> On 1 Apr., 21:52, p...@versatilecoding.com (Pete Becker) wrote:
>>> template <class T1, class T1, class R> works for me. (Aside from the
>>> obvious typo)
>>>
>> for me
>>
>> template <class T1, class T1, class R> ...
>>
>> is not really sound if I write a template that is intended to work not
>> only with class types but also with fundamental and other types. I
>> know that this does work with the current standard but the wording is
>> obviously misleading.
>>
>
> It works with the current standard, past standards, and it will work
> with all future standards. It's not misleading unless you think that
> "class" here means "class and only class," which it doesn't. I have
> yet to hear from anyone other than an absolute beginner who was
> confused by this.
To top that, I think the campaign by many to use "typename" instead of
"class" unless the parameter is actually a class gives the problem
more weight than it deserves, and is actually likely to cause more
confusion among beginners. It's easy to transform a coding guideline,
in your mind, into an assertion about what the language allows. Then
when you see an int being passed to a template declared with "class,"
they're likely to think it's illegal.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
Don't Miss BoostCon 2007! ==> http://www.boostcon.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Joachim Faulhaber" <afojgo@googlemail.com>
Date: Wed, 4 Apr 2007 11:53:35 CST Raw View
On 4 Apr., 00:10, p...@versatilecoding.com (Pete Becker) wrote:
> Joachim Faulhaber wrote:
>
> > With concepts you ought to be more precise in the future
>
> > template <LessThanComparable T> ...
>
> > T is a type that conforms the signatures (and axioms) of concept
> > LessThanComparable. But with keyword class I can be rather imprecise.
>
> > template <class T> ...
>
> > T is a type that can be a class AND ANY OTHER TYPE.
>
> Yes, that's exactly what it means. What's the problem?
>
let me put it this way:
You go to the template restaurant ;) and order a pizza. After a while
the waiter comes and brings you "salad surprise". To your complaints
that you *declared* something different when you ordered the waiter
responds:
"In this restaurant, if someone says pizza it means pizza OR ANYTHING
ELSE, everyone knows that here, so what's the problem"
You may be a little annoyed especially if you'd love pizza and hate
salad ;)
cheers Joachim
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: SeeWebsiteForEmail@erdani.org ("Andrei Alexandrescu (See Website For Email)")
Date: Wed, 4 Apr 2007 18:32:15 GMT Raw View
David Abrahams wrote:
> on Tue Apr 03 2007, pete-AT-versatilecoding.com (Pete Becker) wrote:
>
>> Joachim Faulhaber wrote:
>>> On 1 Apr., 21:52, p...@versatilecoding.com (Pete Becker) wrote:
>>>> template <class T1, class T1, class R> works for me. (Aside from the
>>>> obvious typo)
>>>>
>>> for me
>>>
>>> template <class T1, class T1, class R> ...
>>>
>>> is not really sound if I write a template that is intended to work not
>>> only with class types but also with fundamental and other types. I
>>> know that this does work with the current standard but the wording is
>>> obviously misleading.
>>>
>> It works with the current standard, past standards, and it will work
>> with all future standards. It's not misleading unless you think that
>> "class" here means "class and only class," which it doesn't. I have
>> yet to hear from anyone other than an absolute beginner who was
>> confused by this.
>
> To top that, I think the campaign by many to use "typename" instead of
> "class" unless the parameter is actually a class gives the problem
> more weight than it deserves, and is actually likely to cause more
> confusion among beginners. It's easy to transform a coding guideline,
> in your mind, into an assertion about what the language allows. Then
> when you see an int being passed to a template declared with "class,"
> they're likely to think it's illegal.
I agree. A convention I've tried in Loki was to use template <class T>
whenever T was required to be a struct/class (e.g. policies), and
template <typename T> whenever T could have been a built-in type.
This turned out to be unsuccessful: (1) people got confused, (2)
sometimes I put the wrong thing in (because it's unchecked anyway),
which got people more confused.
My broader conclusion, possibly too general, is that it's unrecommended
to have in a language two exact synonyms. They are bound to waste
everybody's time.
Andrei
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: pete@versatilecoding.com (Pete Becker)
Date: Wed, 4 Apr 2007 18:34:42 GMT Raw View
Joachim Faulhaber wrote:
> On 4 Apr., 00:10, p...@versatilecoding.com (Pete Becker) wrote:
>> Joachim Faulhaber wrote:
>>
>>> With concepts you ought to be more precise in the future
>>> template <LessThanComparable T> ...
>>> T is a type that conforms the signatures (and axioms) of concept
>>> LessThanComparable. But with keyword class I can be rather imprecise.
>>> template <class T> ...
>>> T is a type that can be a class AND ANY OTHER TYPE.
>> Yes, that's exactly what it means. What's the problem?
>>
>
> let me put it this way:
>
> You go to the template restaurant ;) and order a pizza. After a while
> the waiter comes and brings you "salad surprise". To your complaints
> that you *declared* something different when you ordered the waiter
> responds:
> "In this restaurant, if someone says pizza it means pizza OR ANYTHING
> ELSE, everyone knows that here, so what's the problem"
> You may be a little annoyed especially if you'd love pizza and hate
> salad ;)
>
In the context of C++ template arguments, the keyword "class" means
type. Nothing more. That meaning is well documented and well understood.
Indeed, you cited it directly, above.
Is there really any serious contention that using "class" to designate
type arguments causes confusion? Or is this a matter of some
idiosyncratic notion of purity?
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: kuyper@wizard.net
Date: Wed, 4 Apr 2007 16:08:04 CST Raw View
Joachim Faulhaber wrote:
> On 4 Apr., 00:10, p...@versatilecoding.com (Pete Becker) wrote:
> > Joachim Faulhaber wrote:
> >
> > > With concepts you ought to be more precise in the future
> >
> > > template <LessThanComparable T> ...
> >
> > > T is a type that conforms the signatures (and axioms) of concept
> > > LessThanComparable. But with keyword class I can be rather imprecise.
> >
> > > template <class T> ...
> >
> > > T is a type that can be a class AND ANY OTHER TYPE.
> >
> > Yes, that's exactly what it means. What's the problem?
> >
>
> let me put it this way:
>
> You go to the template restaurant ;) and order a pizza. After a while
> the waiter comes and brings you "salad surprise". To your complaints
> that you *declared* something different when you ordered the waiter
> responds:
> "In this restaurant, if someone says pizza it means pizza OR ANYTHING
> ELSE, everyone knows that here, so what's the problem"
> You may be a little annoyed especially if you'd love pizza and hate
> salad ;)
Some key points need to be added to make your analogy applicable:
1) The language used to place orders in the template restaurant is
idiosyncratic - it's used in no other context.
2) Anyone who even attempts to learn that language should, fairly
early in that process, have learned that "pizza" has precisely this
meaning, and has only a weak connection to the word with the same
spelling and pronunciation as used outside the restaurant.
With those additions, your analogy ceases to describe outrageous
behavior on the part of the restaurant. Instead, it's the patron who
seems pretty foolish.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: dave@boost-consulting.com (David Abrahams)
Date: Thu, 5 Apr 2007 17:39:56 GMT Raw View
on Wed Apr 04 2007, SeeWebsiteForEmail-AT-erdani.org ("Andrei Alexandrescu (See Website For Email)") wrote:
>> To top that, I think the campaign by many to use "typename" instead of
>> "class" unless the parameter is actually a class gives the problem
>> more weight than it deserves, and is actually likely to cause more
>> confusion among beginners. It's easy to transform a coding guideline,
>> in your mind, into an assertion about what the language allows. Then
>> when you see an int being passed to a template declared with "class,"
>> they're likely to think it's illegal.
>
> I agree. A convention I've tried in Loki was to use template <class T>
> whenever T was required to be a struct/class (e.g. policies), and
> template <typename T> whenever T could have been a built-in type.
>
> This turned out to be unsuccessful: (1) people got confused, (2)
> sometimes I put the wrong thing in (because it's unchecked anyway),
> which got people more confused.
>
> My broader conclusion, possibly too general, is that it's
> unrecommended to have in a language two exact synonyms. They are bound
> to waste everybody's time.
Yep, and if you have to choose, I say pick the one that isn't
overloaded in this context (between <...>). Check it out:
template <typename T, typename T::nested>
class foo {};
The first typename means one thing (same as 'class'); the second means
something *completely different* (can't substitute "class" for that
one). I always use 'class' where I can so that when "typename" pops
up, there's no confusion about what it's doing.
Besides that, 'class' is shorter. ;-)
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
Don't Miss BoostCon 2007! ==> http://www.boostcon.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Joachim Faulhaber" <afojgo@googlemail.com>
Date: Sat, 31 Mar 2007 18:37:27 CST Raw View
Is there any good reason why keyword typename is 'typename' instead of
just 'type'. As a basic keyword denoting the full abstraction over
fundamental and class types it gains more importance as generic
programming in c++ thrives and it will be even more important with the
introduction of concepts.
I find it annoying to use a lengthy keyword
template <typename T1, typename T1, typename R> ...
when a shorter and more precise wording is obvious
template <type T1, type T1, type R> ...
After all we do not denote classes, structs, unions, enums, ints ...
by classname, structname, unionname, enumname, intname ... ;)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: tazmaster@rocketmail.com ("Jim Langston")
Date: Sun, 1 Apr 2007 09:02:01 GMT Raw View
"Joachim Faulhaber" <afojgo@googlemail.com> wrote in message
news:1175379737.600966.92230@y66g2000hsf.googlegroups.com...
> Is there any good reason why keyword typename is 'typename' instead of
> just 'type'. As a basic keyword denoting the full abstraction over
> fundamental and class types it gains more importance as generic
> programming in c++ thrives and it will be even more important with the
> introduction of concepts.
>
> I find it annoying to use a lengthy keyword
> template <typename T1, typename T1, typename R> ...
>
> when a shorter and more precise wording is obvious
> template <type T1, type T1, type R> ...
>
> After all we do not denote classes, structs, unions, enums, ints ...
> by classname, structname, unionname, enumname, intname ... ;)
Most likely because a variable named "type" would be much more common to
exist in existing C code than one called "typename".
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: pete@versatilecoding.com (Pete Becker)
Date: Sun, 1 Apr 2007 19:52:56 GMT Raw View
Joachim Faulhaber wrote:
> Is there any good reason why keyword typename is 'typename' instead of
> just 'type'. As a basic keyword denoting the full abstraction over
> fundamental and class types it gains more importance as generic
> programming in c++ thrives and it will be even more important with the
> introduction of concepts.
>
> I find it annoying to use a lengthy keyword
> template <typename T1, typename T1, typename R> ...
>
template <class T1, class T1, class R> works for me. (Aside from the
obvious typo)
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "jam" <farid.mehrabi@gmail.com>
Date: Mon, 2 Apr 2007 09:42:01 CST Raw View
On Apr 1, 10:52 pm, p...@versatilecoding.com (Pete Becker) wrote:
> Joachim Faulhaber wrote:
> > Is there any good reason why keyword typename is 'typename' instead of
> > just 'type'. As a basic keyword denoting the full abstraction over
> > fundamental and class types it gains more importance as generic
> > programming in c++ thrives and it will be even more important with the
> > introduction of concepts.
>
> > I find it annoying to use a lengthy keyword
> > template <typename T1, typename T1, typename R> ...
>
> template <class T1, class T1, class R> works for me. (Aside from the
> obvious typo)
>
> --
>
> -- Pete
typename keyword`s use is more than just that:
template <typename traits>
struct A{
typedef typename /*cannot be replaced with class*/
traits::nested mynested;
};
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: pete@versatilecoding.com (Pete Becker)
Date: Mon, 2 Apr 2007 16:42:38 GMT Raw View
jam wrote:
> On Apr 1, 10:52 pm, p...@versatilecoding.com (Pete Becker) wrote:
>> Joachim Faulhaber wrote:
>>> I find it annoying to use a lengthy keyword
>>> template <typename T1, typename T1, typename R> ...
>> template <class T1, class T1, class R> works for me. (Aside from the
>> obvious typo)
>>
>> --
>>
> typename keyword`s use is more than just that:
>
Nevertheless, in this example, it is not necessary, so the complaint
that it's too lengthy is unjustified.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]