Re: C++ as a target language
* skaller:
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.
Yep. I think it's beyond hope to get a new, alternative typedef syntax
in place. In an ideal world, perhaps one could get a 'typename'
construct added, used like (compare to real code in previous posting)
<hypotheticalcode>
#include <utility> // std::pair
#include <boost/shared_ptr.hpp> // boost::shared_ptr
namespace stdx = boost;
template< typename T >
struct List
{
typename Node; // NEW LANGUAGE FEATURE, recursive defs.
typedef stdx::shared_ptr<Node> Instance;
typedef std::pair<T, Instance> Node;
static Instance null() { return Instance(); }
static Instance cons( T const& v, Instance list )
{
return Instance( new :Node( v, list ) );
}
};
#include <iostream>
#include <ostream>
int main()
{
typedef List<int> IntList;
using class IntList; // NEW LANGUAGE FEATURE, less verbiage.
for( Instance list =
cons( 1, cons( 2, cons( 3, null() ) ) );
list;
list = list->second )
{
std::cout << list->first << std::endl;
}
}
</hypotheticalcode>
Or, if we get templated namespaces, then less need for 'using class'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
---
[ 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 ]