Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-10-27 19:39:59

----- Original Message -----
From: "Mark Evans" <mark.evans_at_[hidden]>
To: "David Abrahams" <abrahams_at_[hidden]>
Sent: Friday, October 27, 2000 6:23 PM
Subject: C++ Pythonic Classes in STL

> David,
> One of the claims to fame of CXX is that its Pythonic C++ classes
> (Dict, Tuple, String, etc.) plug and play with STL. I notice in
> py_cpp source "TODO" statements having to do with
> STL iterators. This is one area where CXX might be able to
> contribute something. CXX defines a base class "sequence" type
> which works with STL, and the others derive from it.

Except that technically, it doesn't work with STL. We have discussed this in
the C++ standards committee, and have concluded that in general there is no
way to make a container that requires proxy references (as does any C++
implementation of a Python container) work with the standard library
algorithms. For example, any sort algorithm that tries to swap elements
using std::swap() or the equivalent will fail, e.g.

class List { ...
  struct reference { // the proxy
     reference operator=(Ptr rhs); // assign a list element
     operator Ptr() const;
  struct iterator {
     reference operator*() const;
  iterator begin();
  iterator end();

std::sort() has a right to assume that when a mutable iterator is
dereferenced, the result is a reference type. For example, many sort()
implementations will call std::swap(*i, *j) where i and j are iterators. The
signature for std::swap() is:

    template <class T> void swap(T& x, T& y)

I have spoken to the CXX maintainers about this, but apparently they were
unmoved to correct their documentation. I'm not sure I want to make promises
I can't actually keep ;-)

> I think py_cpp is top-heavy with C++ namespaces. The "hello,
> world" example takes too much effort to understand by
> virtue of namespaces which share common names with
> objects and functions.

There is only one namespace involved (namespace hello). The idea is to
illustrate that module scoping in Python can mirror namespace scoping in
C++. I don't think this is so complicated as to be confusing.

> Please use namespaces as sparingly as
> possible in both source and examples. All they are meant to
> achieve is symbol conflict avoidance between vendor libraries,
> and not much more than that.

Thanks for your advice, but I don't think I'll take it. Modern
best-practices C++ puts everything in a namespace. Even vendor libraries can
suffer from unexpected interactions with user code in the global namespace.
Additionally, boost culture puts implementation details into a nested
namespace called detail, in case users should (despite our advice to the
contrary) write a using-directive such as "using namespace boost" or "using
namespace py". More and more of my code will be moved into namespace detail
(and the unnamed namespace) as I prepare it for boost release.


Boost list run by bdawes at, gregod at, cpdaniel at, john at