[C++-sig] container construction

David Abrahams david.abrahams at rcn.com
Tue Jun 18 23:21:50 CEST 2002


So I'm thinking about how we're going to build lists, tuples, and
dictionaries from C++.

Clearly, there should be a constructor that takes a single object argument
and checks to make sure it's an object of the corresponding Python type,
raising an exception otherwise.

Now, what about creating lists/tuples of more than one argument?
It would be lovely to be able to write:

    list(3, 5, 7) // i.e. [3, 5, 7]

but python doesn't work that way, and furthermore in that case it wouldn't
make much sense to have:

    list(3) // error, 3 is not a list

Furthermore, it should be possible to write:

    list(some_tuple)

as you can in Python. So, I guess we need

    make_list(3, 5, 7) -> [3, 5, 7]

and

    make_tuple('x', 2) -> ('x', 2)

Other interfaces are possible, though:

    tuple(items = 'x', 2)
    list(items = 3, 5, 7)

I'm open to suggestion here.

What about dictionaries? Since:

    >>> dict(((1,'one'), (2,'two')))
    {1: 'one', 2: 'two'}

I guess we could write:

    dict(make_tuple(make_tuple(1,"one"), make_tuple(2,"two")))

but man, that's ugly!

Other possibilities:

    dict(1, "one").add(2, "two")   // chaining
    dict(1, "one"), dict(2, "two") // overloaded comma operator
    dict(1, 'one', 2, 'two')       // even arities only

I'm somewhat inclined toward the last one.

Thoughts?

-Dave
+---------------------------------------------------------------+
                  David Abrahams
      C++ Booster (http://www.boost.org)               O__  ==
      Pythonista (http://www.python.org)              c/ /'_ ==
  resume: http://users.rcn.com/abrahams/resume.html  (*) \(*) ==
          email: david.abrahams at rcn.com
+---------------------------------------------------------------+






More information about the Cplusplus-sig mailing list