Re: C++ as a target language
On Fri, 21 Jul 2006 01:59:45 -0600, Alf P. Steinbach wrote:
If you have a pair template, you want this in C++
template<class T>
typedef list<T> = pair<T, list<T>*>;
There is no way to do this in C++ at all.
I'm not sure what you mean,
'no way to do it' isn't clear? :)
but sitting down at the keyboard and
implementing a little "functional-language-like" list (instead of just
using std::list ;-) ) based on std::pair, I found the following language
limitations:
* no template typedef (is coming in C++0x, AFAIK)
Be nice to have confirmation of that.
Has it passed WG21 yet?
* verbiage (as I understand it, is also addressed)
Verbiage will never be fully addressed for C++.
Improvements will be nice though. The 'auto' deduction
will be very cool.
More?
<code>
#include <utility> // std::pair
#include <boost/shared_ptr.hpp> // boost::shared_ptr
namespace stdx = boost;
template< typename T >
struct List
{
struct Node;
typedef stdx::shared_ptr<Node> Instance;
struct Node: std::pair< T, Instance >
{
typedef std::pair< T, Instance > Base;
Node() {}
Node( T v, Instance list ): Base( v, list ) {}
};
static Instance null()
{
static Instance theNull;
return theNull;
}
};
I think this relies on a trick that only works in
special cases (though I'm not 100% sure).
Instead of writing
typedef list<T> = pair<T, list<T>* >;
you write:
Node : pair <T, Node* >
where : means 'inheritance'. At this point, Node is
incomplete. This is 'open recursion'. You then
close the recursion by completing the type.
Rather unfortunate you have to define a class to
do that, and can't freely write type expressions.
You can do this:
pair< T, pair< U, ptr<V> > >
for any pre-existing types T, U, V, but have to go through
hoops when recursion is required.
In some sense you bypassed the real requirement: to be
able to specify types in combinator form. In particular
the above construction forces you to lift the type definition
out of local code into the global scope (since local classes
don't have extern linkage and so can't be used in templates).
There is also another defect: list is a variant,
but you have cheated and used a special case which is
already a variant, namely the moral equivalent of:
ptr<T> = NULL | Ptr_to_real_object T
but again .. this is probably a variant issue not
a recursion one. Nice job actually!
--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net
---
[ 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 ]