K
Keith H Duggar
Often I've felt that the ability to alias an existing type where the
alias would be treated as a different type thus allowing overloading,
etc would be very useful.
typedef : alias an existing type Foo as Bar where Bar is a Foo
typecpy : alias an existing type Foo as Bar where Bar is not a Foo
Here is a simple example :
typecpy std::valarray<double> RowVector ;
typecpy std::valarray<double> ColVector ;
This would allow you grab all the existing functionality of valarray
and at the same time overload, for example, the arithmetic operators
to act differently on column and row vectors. If you try to gain this
funtionality by inheritance :
class RowVector : public std::valarray<double> { ... }
class RowVector : public std::valarray<double> { ... }
then you are forced to tediously rewrite or forward all of the
valarray constructors.
Actually, in my experience I have run into many cases where typecpy
would save time, code, and effort. I know at least some others share
this feeling. For example in a 1999 thread suggesting the removal of
typedef Dave Harris wrote :
Do any of you think you would benefit from typecpy or think it is a
good idea? Any not?
I'm no compiler expert but it seems that this extension would be
almost trivial to implement. Is that accurate?
The only sticky point I can think of would be in deciding how typecpy
would interact with inheritance. For example :
class Base { ... } ;
class Derv : public Base { ... } ;
typecpy Derv Copy ;
Should Copy be considered a Base?
It seems to me the easiest solution would be to say that Copy is a
Base. In other words, Copy is exactly what you would get if you
exactly copied Derv's declaration and definition and then replaced the
word Derv with Copy.
Wouldn't that be a very easy and useful extension to implement?
Can you think of cases were typecpy would be useful to you?
alias would be treated as a different type thus allowing overloading,
etc would be very useful.
typedef : alias an existing type Foo as Bar where Bar is a Foo
typecpy : alias an existing type Foo as Bar where Bar is not a Foo
Here is a simple example :
typecpy std::valarray<double> RowVector ;
typecpy std::valarray<double> ColVector ;
This would allow you grab all the existing functionality of valarray
and at the same time overload, for example, the arithmetic operators
to act differently on column and row vectors. If you try to gain this
funtionality by inheritance :
class RowVector : public std::valarray<double> { ... }
class RowVector : public std::valarray<double> { ... }
then you are forced to tediously rewrite or forward all of the
valarray constructors.
Actually, in my experience I have run into many cases where typecpy
would save time, code, and effort. I know at least some others share
this feeling. For example in a 1999 thread suggesting the removal of
typedef Dave Harris wrote :
Sometimes I want a new type, and sometimes I want a new name for an
existing type. Not having a good mechanism for doing the former is a
problem, but typedef is still needed for the latter.
Ideally we might have 2 keywords, eg:
typedef - define a new type.
typename - give a new name to an existing type.
although for backwards compatibility typedef is unlikely to change now.
Do any of you think you would benefit from typecpy or think it is a
good idea? Any not?
I'm no compiler expert but it seems that this extension would be
almost trivial to implement. Is that accurate?
The only sticky point I can think of would be in deciding how typecpy
would interact with inheritance. For example :
class Base { ... } ;
class Derv : public Base { ... } ;
typecpy Derv Copy ;
Should Copy be considered a Base?
It seems to me the easiest solution would be to say that Copy is a
Base. In other words, Copy is exactly what you would get if you
exactly copied Derv's declaration and definition and then replaced the
word Derv with Copy.
Wouldn't that be a very easy and useful extension to implement?
Can you think of cases were typecpy would be useful to you?