[C++-sig] wrapping typedef equivalent types

David Abrahams dave at boost-consulting.com
Wed Oct 19 20:14:02 CEST 2005


Neal Becker <ndbecker2 at gmail.com> writes:

> David Abrahams wrote:
>
>> Neal Becker <ndbecker2 at gmail.com> writes:
>> 
>>> What happens if, using boost::python, I try to wrap 2 type equivalent
>>> classes?
>>>
>>> For example (this is a trivial example):
>>>
>>> class_<int>...
>>> class_<int32_t>...
>>> where int == int32_t
>>>
>>> My real objective is to wrap classes
>>>
>>> template<int_t> class X {};
>>>
>>> where int_t is
>>> int (natural int size)
>>> int32_t (force 32-bit)
>>> int64_t (force 64-bit)
>>>
>>> Can wrapped X<int>, X<int32_t>, X<int64_t> co-exist in the same python
>>> module?
>> 
>> No, all wrapped classes have to have different types.  You could add a
>> dummy parameter to X in order to distinguish them.
>> 
>
> I don't understand the dummy parameter suggestion.  Could you elaborate?

template <class int_t, int bits> class X {};

  X<int, 16>
  X<int32_t, 32>
  X<int64_t, 64>

or

  template <int bits>
  class X
  {
     typedef boost::int_t<bits>::fast int_t;
     ...
  };
  
> I found a different solution.  I just expose the int32_t and int64_t
> versions, then use python code to set the equivalence.  In python, I write 
> something like:
>
> if (intsize() == 4): X = X32
>
> I guess there's nothing wrong with that approach :)

Not unless X32 and X64 are trying to be wrappers for the same
underlying C++ class.

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




More information about the Cplusplus-sig mailing list