[C++-sig] Boost tuple example?

David Abrahams dave at boost-consulting.com
Fri Dec 9 16:19:28 CET 2005


"Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> writes:

>>      tuple Values("value 1", 2, 3.0);
>
> Does this work? I was thinking you need
>
>        tuple Values = make_tuple("value 1", 2, 3.0);
>
> but I am not sure.

Ralf is right.

>>     try
>>     {
>
> I don't think it is advisable to use try + catch() here. Just let the exception
> propagate through to Python.

Ralf is right.

>>           const char * szVal = extract<const char *>(oValues[0]);
>>           int nVal = extract<int>(oValues[1]);
>>           double nDblVal = extract<double>(oValues[2]);
>
> I believe this works on most compilers, but not all. I always do it like this:
>
>             const char * szVal = extract<const char *>(oValues[0])();
>
> I.e. with an additional "()" at the end. Similar for the two other extract<>
> statements.

Ralf, I'm really surprised.  AFAIK the only compilers that need that
syntax are vc6/7, which IIUC your code doesn't support.

>>     }
>>     catch(...)
>>     {
>>           PyErr_Print();
>>     }
>> }
>> 
>> Does that seem reasonable?
>> 
>> Also, is there an easy way to determine the length of a tuple in C++?
>
> David insisted on not giving us .size(), much to my dismay. Therefore you have
> to say:
>
>   #include <boost/python/detail/api_placeholder.hpp>
>
> and then:
>
>   boost::python::len(obj)
>
> David: I still believe we should add .size() to tuple, list, str. The current
> solution is a real productivity killer; you won't believe how much time I
> spent/wasted over the years digging out that silly include above again and
> again. Purity is not always a virtue.

You could go ahead and add it, but it's not going to work on
boost::python::object (unless you add it there, which is *really*
impure!)

Wouldn't it be better just to make a single len function available in
the same namespace as boost::python::object, and in the same header,
so you can

    len(obj)

without qualification?  Seems more natural and broadly applicable to
me.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list