 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ron House Guest
|
Posted: Tue May 10, 2005 4:37 am Post subject: Suggestion: Compile-time "typeof" |
|
|
It often happens that one want to propagate an argument or an object
type as the result of a function. When the arg is a ref or ptr, the
actual arg might not be the declared type, but a descendant. It would be
very useful to be able to propagate that information into the result
type. For example,
typeof(out) operator<<(ostream& out, const Person& p) {
... output the Person data ...
return out;
}
Now, if the << operator is used as part of a larger expression, the
compiler might be able to deduce stronger limits on the type of the
entire expression than it can if the result type is merely ostream.
I ran into this one while writing a parent class with methods that were
designed to be 'strung together', each returning a ref to the owning
object, *this. The classes would string together nicely, but when used
in a descendant, the compiler thought the type was the ancestor, so I
had to write inline 'shell' functions in every descendant to merely call
the ancestor but give it the more descriptive type. It would have been
great to be able to write something like:
typeof(*this)& mymethod() { ... return *this; }
Of course, I don't think it is beyond credibility that I have entirely
overlooked some way that already exists to accomplish this...
--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Max T. Woodbury Guest
|
Posted: Wed May 11, 2005 1:34 am Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Ron House wrote:
| Quote: | It often happens that one want to propagate an argument or an object
type as the result of a function. When the arg is a ref or ptr, the
actual arg might not be the declared type, but a descendant. It would be
very useful to be able to propagate that information into the result
type. For example,
typeof(out) operator<<(ostream& out, const Person& p) {
... output the Person data ...
return out;
}
Now, if the << operator is used as part of a larger expression, the
compiler might be able to deduce stronger limits on the type of the
entire expression than it can if the result type is merely ostream.
I ran into this one while writing a parent class with methods that were
designed to be 'strung together', each returning a ref to the owning
object, *this. The classes would string together nicely, but when used
in a descendant, the compiler thought the type was the ancestor, so I
had to write inline 'shell' functions in every descendant to merely call
the ancestor but give it the more descriptive type. It would have been
great to be able to write something like:
typeof(*this)& mymethod() { ... return *this; }
Of course, I don't think it is beyond credibility that I have entirely
overlooked some way that already exists to accomplish this...
|
GRIN! You really should search the archives. I proposed almost exactly
this quite some time ago only to discover that the GNU compiler already
had something like it. Further, you can apparently get this information
with an appropriate specified template class.
[email]max (AT) mtew (DOT) isa-geek.net[/email]
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Daniel Frey Guest
|
Posted: Wed May 11, 2005 8:58 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Ron House wrote:
| Quote: | type. For example,
typeof(out) operator<<(ostream& out, const Person& p) {
... output the Person data ...
return out;
}
Now, if the << operator is used as part of a larger expression, the
compiler might be able to deduce stronger limits on the type of the
entire expression than it can if the result type is merely ostream.
I ran into this one while writing a parent class with methods that were
designed to be 'strung together', each returning a ref to the owning
object, *this. The classes would string together nicely, but when used
in a descendant, the compiler thought the type was the ancestor, so I
had to write inline 'shell' functions in every descendant to merely call
the ancestor but give it the more descriptive type. It would have been
great to be able to write something like:
typeof(*this)& mymethod() { ... return *this; }
Of course, I don't think it is beyond credibility that I have entirely
overlooked some way that already exists to accomplish this...
|
You mean like:
template< typename T >
T& operator<<( T& out, const Person& p ) { ...; return out; }
Obviously this doesn't solve the general case you addressed, but I guess
your example is out ;)
Regards, Daniel
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Wed May 11, 2005 8:58 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Ron House wrote:
| Quote: | It often happens that one want to propagate an argument or an object
type as the result of a function. When the arg is a ref or ptr, the
actual arg might not be the declared type, but a descendant. It would be
very useful to be able to propagate that information into the result
type. For example,
typeof(out) operator<<(ostream& out, const Person& p) {
... output the Person data ...
return out;
}
Now, if the << operator is used as part of a larger expression, the
compiler might be able to deduce stronger limits on the type of the
entire expression than it can if the result type is merely ostream.
I ran into this one while writing a parent class with methods that were
designed to be 'strung together', each returning a ref to the owning
object, *this. The classes would string together nicely, but when used
in a descendant, the compiler thought the type was the ancestor, so I
had to write inline 'shell' functions in every descendant to merely call
the ancestor but give it the more descriptive type. It would have been
great to be able to write something like:
typeof(*this)& mymethod() { ... return *this; }
Of course, I don't think it is beyond credibility that I have entirely
overlooked some way that already exists to accomplish this...
|
typeof has been implemented by some compilers; the proposal under
consideration by the committee for the next C++ standard uses the
keyword "decltype" for something along similar lines.
-- James
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Wed May 11, 2005 8:59 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Max T. Woodbury wrote:
| Quote: | Ron House wrote:
GRIN! You really should search the archives. I proposed almost exactly
this quite some time ago only to discover that the GNU compiler already
had something like it. Further, you can apparently get this information
with an appropriate specified template class.
|
You really should search the archives, too. There is an ongoing formal
proposal of a typeof-like core language feature, called decltype. You
can have a read about it here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf
The paper also contains the rationale why the proposed name is not
typeof and much more (a new use of the auto keyword, a new syntax for
specifying function return values, etc.)
Alberto
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Thu May 12, 2005 2:33 am Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Ron House schreef:
| Quote: | It often happens that one want to propagate an argument or an object
type as the result of a function. When the arg is a ref or ptr, the
actual arg might not be the declared type, but a descendant. It would
be
very useful to be able to propagate that information into the result
type. For example,
typeof(out) operator<<(ostream& out, const Person& p) {
... output the Person data ...
return out;
}
|
That is of course impossible. The returned object has a static and
dynamic type. Clearly, ostream& is the static type, and the dynamic
type is whatever is passed in at runtime. You cannot make the static
type identical to the dynamic type, because (a) there can be multiple
dynamic types, for each call to operator<< while there's only one
static
type and (b) static types are determined before dynamic types are even
known.
HTH,
Michiel Salters
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ron House Guest
|
Posted: Thu May 12, 2005 6:06 am Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Alberto Barbati wrote:
| Quote: | Max T. Woodbury wrote:
Ron House wrote:
GRIN! You really should search the archives. I proposed almost exactly
this quite some time ago only to discover that the GNU compiler already
had something like it. Further, you can apparently get this information
with an appropriate specified template class.
You really should search the archives, too. There is an ongoing formal
proposal of a typeof-like core language feature, called decltype. You
can have a read about it here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf
The paper also contains the rationale why the proposed name is not
typeof and much more (a new use of the auto keyword, a new syntax for
specifying function return values, etc.)
|
Thanks for that ref. Having read it, though, I see they are not actually
solving the problem I am raising. Their proposal is largely syntactic
sugar. Their decltype gives the type of the formal parameter, not the
(compile-time) type of the actual parameter, and is therefore vastly
less useful. What I want is this:
class A {
...
A& mymethod() { ... return *this; }
};
class B: public A {
...
B& bmethod() { ... }
};
B b;
.. b.mymethod() <--- at this point the compiler knows the expression
result is a B&, not an A&.
The code as written clearly tells the compiler that mymethod returns an
A&, but in fact static examination of the code promises that mymethod is
actually returning a B& at this point. My suggested rewriting of mymethod:
typeof(*this)& mymethod() { ... return *this; }
ensures that no other returns can be inserted returning supertypes of B,
because, in order to write a return that satisfies typeof(*this)&, it
more or less can't be anything but *this. Any actual typed expression
(such as an A variable) _might_ be a supertype of *this at runtime. So
consider:
A& mymethod() {
if (...) return somethingoftypeA; else return *this;
}
The above is syntactically correct, and we have no guarantee that
mymethod will, for a B object, return a B. The compiler must treat the
result as of type A, and the following will be illegal:
b.mymethod().bmethod();
However, if we could write:
typeof(*this)& mymethod() {
if (...) return somethingoftypeA; else return *this;
}
then the compiler would give a syntax error because it cannot guarantee
that somethingoftypeA will indeed have a type compatible with *this at
runtime (because the method might be executed from a descendant).
However, the simpler version:
typeof(*this)& mymethod() { ... return *this; }
will be correct, and can be guaranteed at compile time to actually
return something equal or a subtype of its owning object at runtime.
Then the call:
b.mymethod().bmethod();
will be seen to be sane and will compile correctly.
Aikens designed a simple language called Cool for his compiler design
class, and he convinced me with it that he is a genius! Cool does all
this right, despite being a 'toy' language. There is no reason at all
C++ should get it wrong. (See http://www.cs.berkeley.edu/~aiken/cool)
--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ron House Guest
|
Posted: Thu May 12, 2005 6:06 am Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Daniel Frey wrote:
| Quote: | You mean like:
template< typename T
T& operator<<( T& out, const Person& p ) { ...; return out; }
Obviously this doesn't solve the general case you addressed, but I guess
your example is out
|
Yes, that one does seem to solve the problem when there is an explicit
argument, although it leaves the method return type out in the cold. And
it sort of rankles to use a template for such a simple requirement.
--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ron House Guest
|
Posted: Mon May 16, 2005 4:19 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
James Dennett wrote:
| Quote: | typeof has been implemented by some compilers; the proposal under
consideration by the committee for the next C++ standard uses the
keyword "decltype" for something along similar lines.
|
Unfortunately the similar lines are not similar enough to solve the
major problem. See my other post today.
--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ron House Guest
|
Posted: Mon May 16, 2005 4:19 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
msalters wrote:
| Quote: | Ron House schreef:
It often happens that one want to propagate an argument or an object
type as the result of a function. When the arg is a ref or ptr, the
actual arg might not be the declared type, but a descendant. It would
be
very useful to be able to propagate that information into the result
type. For example,
typeof(out) operator<<(ostream& out, const Person& p) {
... output the Person data ...
return out;
}
That is of course impossible. The returned object has a static and
dynamic type. Clearly, ostream& is the static type, and the dynamic
type is whatever is passed in at runtime.
|
I never mentioned the dynamic type, I mentioned the type of the actual
parameter, meaning the declared type as known to the compiler.
I think you misunderstand the requirement. I am not asking that the
compiler use magic to know what the dynamic type will be at runtime. But
the compiler very well will know the declared type of the actual
parameter at compile time, and that (in order to allow at runtime
descendants to be passed) might be a descendant of the type of the
formal parameter. It will know statically that the method returns that
descendant type. But C++ doesn't allow me to specify this to the
compiler, entirely due a language shortcoming, nothing to do with
runtime types at all. And the decltype suggestion does NOT meet that need.
--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Mon May 16, 2005 4:19 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Ron House wrote:
| Quote: | typeof(*this)& mymethod() {
if (...) return somethingoftypeA; else return *this;
}
|
I doubt the utility of returning a reference to a different object
of the same type as the caller, so I would refine this proposal
to simply allow a method to be declared of type 'this', and have
such a method implicitly return *this without needing it to be
explicitly specified. It's the perfect return type for assignment
operators, as well as the stream pass-throughs.
struct mystream : ostream {
template <typename T>
this operator<<(T v) { (ostream &)*this << v; }
};
struct point {
double x, y, z;
this operator=(const point &o) { x = o.x; y = o.y; z = o.z; }
};
And the static type of a 'this' method invocation will of course be
the static type of the class to which the initial invoking pointer,
reference, or actual object refers.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Mon May 16, 2005 4:21 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Ron House wrote:
| Quote: | Alberto Barbati wrote:
You really should search the archives, too. There is an ongoing formal
proposal of a typeof-like core language feature, called decltype. You
can have a read about it here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf
The paper also contains the rationale why the proposed name is not
typeof and much more (a new use of the auto keyword, a new syntax for
specifying function return values, etc.)
Thanks for that ref. Having read it, though, I see they are not actually
solving the problem I am raising. Their proposal is largely syntactic
sugar.
|
I'm not sure the authors of that proposal are very happy the hear
that... ;-)
| Quote: | sugar. Their decltype gives the type of the formal parameter, not the
(compile-time) type of the actual parameter, and is therefore vastly
less useful. What I want is this:
class A {
...
A& mymethod() { ... return *this; }
};
class B: public A {
...
B& bmethod() { ... }
};
B b;
. b.mymethod() <--- at this point the compiler knows the expression
result is a B&, not an A&.
|
Hmmmm... So you are telling me that you want the *same* function
A::mymethod to return two different types and the compiler should choose
which type according to type of one of the functions parameters (the
implicit "this" parameter, in this case)?
I'm not saying that it couldn't be done, but:
1) if it's limited to typeof(*this), then it's just some kind of limited
static covariance. It might even be easy to implement, although
requiring a new keyword for this feature looks a bit too much to me.
Frankly I don't see a case where this feature could really be useful.
Could you please enlighten me by posting a *real-life* use case where
this idiom could effectively help?
Moreover, consider this:
class A {
...
A& mymethod() { ... return *this; }
};
template
T& mymethod(T& x) { x.mymethod(); return x; }
and just use mymethod(b) instead of b.mymethod().
2) if this feature is taken in full generality (any expression in typeof
depending on any parameter) I find it hard to believe that such thing
can be implemented without troubles...
| Quote: |
Aikens designed a simple language called Cool for his compiler design
class, and he convinced me with it that he is a genius! Cool does all
this right, despite being a 'toy' language. There is no reason at all
C++ should get it wrong. (See http://www.cs.berkeley.edu/~aiken/cool)
|
I have no reason to doubt about the intelligence of this person, but the
fact that you don't see a reason why C++ does not have such a feature
does not imply that they don't exist, neither that C++ is "wrong" about
it. You have to be more convincing than that.
Alberto
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Mon May 16, 2005 10:04 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
[email]hyrosen (AT) mail (DOT) com[/email] (Hyman Rosen) wrote (abridged):
| Quote: | typeof(*this)& mymethod() {
if (...) return somethingoftypeA; else return *this;
}
I doubt the utility of returning a reference to a different object
of the same type as the caller, so I would refine this proposal
to simply allow a method to be declared of type 'this', and have
such a method implicitly return *this without needing it to be
explicitly specified.
|
Interesting digression, but I think the original proposal was supposed to
work for non-member functions too. It would be handy if:
typeof( str ) strchr( const char *str, char ch );
char str[10] = "test";
char *s = strchr( str, 's' );
*s = 'x';
compiled. This example doesn't use classes or inheritance at all.
Returning an object of the same type as an argument, but not the same
object, is useful sometimes. For example, the intersection of one
rectangle with another rectangle is always a rectangle. It may or may not
be equal to one or both argument rectangles, and if it is equal, it may or
may not be the same object.
However, if it is a different object, I doubt the compiler could
statically prove it was the same type, in general.
-- Dave Harris, Nottingham, UK.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Mon May 16, 2005 11:15 pm Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Dave Harris wrote:
| Quote: | Interesting digression
|
The sneaky part of my suggestion, if you looked at my
code sample, was that such functions looked very much
like they could be declared as returning void. And they
could be, because all the compiler has to do is at the
call site, make believe that the function has returned
the call object (or in your example, some distinguished
parameter).
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Tue May 17, 2005 1:59 am Post subject: Re: Suggestion: Compile-time "typeof" |
|
|
Dave Harris wrote:
| Quote: |
Interesting digression, but I think the original proposal was supposed to
work for non-member functions too. It would be handy if:
typeof( str ) strchr( const char *str, char ch );
char str[10] = "test";
char *s = strchr( str, 's' );
*s = 'x';
compiled. This example doesn't use classes or inheritance at all.
Returning an object of the same type as an argument, but not the same
object, is useful sometimes. For example, the intersection of one
rectangle with another rectangle is always a rectangle. It may or may not
be equal to one or both argument rectangles, and if it is equal, it may or
may not be the same object.
|
Why not use template?
template <typename Char>
Char* strchr(Char* str, char ch);
--
Seungbeom Kim
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|