Topic: typename rationale question
Author: "Brad Daniels" <brad.daniels@missioncritical.com>
Date: 1998/12/16 Raw View
I like the introduction of the typename keyword for template parameters,
since the parameter type may not be classes, but I'm a bit confused as to
why typename can only be used in that limited context. It seems completely
natural and highly desirable to use it for forward declarations as well.
For example:
typename A;
class B {
A *aPtr;
//...
};
At this point, A could be an enumerator, a class, a built-in type, or a
typedef. Class B doesn't care or need to know what A actually is, other
than that it is a type. I have run into this problem more than once where I
decided to re-implement a class as a typedef for a template specialization
or a more generic class, and then had to jump through all kinds of hoops to
replace my "class A" forward declarations with something appropriate. In
many cases I had to actually include a header file just to get the damn type
name visible.
Does the restriction have to do with overloading? I realize that compilers
would have to change their handling of typedefs such that an overloaded
function that uses a typedef name would have multiple 'alias' identifiers in
the output, but that issue seems far more tractible than many of the
problems introduced by templates.
- Brad
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Ross Smith" <ross.s@ihug.co.nz>
Date: 1998/12/16 Raw View
Brad Daniels wrote in message <758pdf$gq7$1@news1-alterdial.uu.net>...
>
>I like the introduction of the typename keyword for template parameters,
>since the parameter type may not be classes, but I'm a bit confused as to
>why typename can only be used in that limited context. It seems completely
>natural and highly desirable to use it for forward declarations as well.
>For example:
>
>typename A;
>
>class B {
> A *aPtr;
>//...
>};
>
>At this point, A could be an enumerator, a class, a built-in type, or a
>typedef. Class B doesn't care or need to know what A actually is, other
>than that it is a type.
Not true, I'm afraid. All class pointers are required to have the same
implementation (sometimes expressed as "all classes smell the same"),
which is why you can get away with just saying "class A" in this sort
of context; knowing that A is a class gives the compiler enough
information to handle pointers to A correctly (provided they're not
dereferenced, obviously).
But that's not true of pointers to other types. Pointers to different
arbitrary types, which may or may not be classes, are *not* required
to have the same implementation. Merely knowing that A is the name of
a type is not enough information for the compiler to know how to handle
an A*.
On most modern 32-bit microprocessor systems, all pointers usually do
have the same representation. But that's just an artifact of a specific
kind of byte-addressable architecture, not a universal law. On a
word-addressable machine, for example, pointers to objects smaller than
a word (e.g. char*) will often have extra bits in them.
--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
* * * * *
To err is human. To really screw up requires the root password.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1998/12/16 Raw View
Brad Daniels wrote in message <758pdf$gq7$1@news1-alterdial.uu.net>...
>
>I like the introduction of the typename keyword for template parameters,
>since the parameter type may not be classes, but I'm a bit confused as to
>why typename can only be used in that limited context. It seems completely
>natural and highly desirable to use it for forward declarations as well.
>For example:
>
>typename A;
>
>class B {
> A *aPtr;
>//...
>};
>
>At this point, A could be an enumerator, a class, a built-in type, or a
>typedef.
In C and C++ all pointers to structs are compatible with each other. In
particular they all have the same size. Pointers to other kinds of objects
aren't necessarily compatible.
Of course all data pointers are convertible to and from (void*), but that
conversion is not necessarily efficient.
Your proposal would require compilers to use the (possibly inefficient)
void* representation for essentially all pointers (or to go back and fix-up
pointers after all the code has been seen, say with a smart linker).
In addition to the above problems, pointer to typedef doesn't work because
typedefs never have external linkage. It is perfectly legal for me to have
typedef int A;
in one file and
typedef long double A;
in another file, both in the same namespace.
What kind of pointer does B (which does have external linkage) hold in this
case?
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/12/17 Raw View
In article <758pdf$gq7$1@news1-alterdial.uu.net>, Brad Daniels
<brad.daniels@missioncritical.com> writes
>I like the introduction of the typename keyword for template parameters,
>since the parameter type may not be classes, but I'm a bit confused as to
>why typename can only be used in that limited context. It seems completely
>natural and highly desirable to use it for forward declarations as well.
>For example:
>
>typename A;
>
>class B {
> A *aPtr;
>//...
>};
>
>At this point, A could be an enumerator, a class, a built-in type, or a
>typedef.
Well, yes it does. We are not allowed to forward declare enums (because
different possible underlying integer types may have different pointers)
Likewise if A were a builtin type or a typedef to such the compiler
lacks the info to even create an appropriate pointer.
Forward declaration is strictly for class (and the variants of struct
and union) types.
>Class B doesn't care or need to know what A actually is, other
>than that it is a type. I have run into this problem more than once where I
>decided to re-implement a class as a typedef for a template specialization
>or a more generic class, and then had to jump through all kinds of hoops to
>replace my "class A" forward declarations with something appropriate. In
>many cases I had to actually include a header file just to get the damn type
>name visible.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]