Boost logo

Boost :

Subject: Re: [boost] [tuple] Type composition
From: Andrew Hundt (athundt_at_[hidden])
Date: 2012-07-20 10:26:56


The goal of this idea sounds great to me. It works to solve one of the
exact complaints I've received when I try to sell coworkers on the idea of
using tuples.

Cheers!
Andrew Hundt

On Fri, Jul 20, 2012 at 12:52 AM, Amir Ansari <amir.ansari80_at_[hidden]>wrote:

> I like to reuse tuple<> to quickly create new types as:
>
> class my_data : public boost::tuple<int, int, bool, int> {
>
> // The following could go into a macro:
>
> enum { Id, Price, Sellable, Shares, Gain }
> // Ensure copy constructor
> my_data(const my_data & d) : boost::tuple<int, int, bool, int>(d) {}
>
> };
>
> The enum and copy constructor could be defined using a macro whose
> signature is:
>
> HANDLE_TUPLE4(Class, Type1, Name1, Type2, Name2, Type3, Name3, Type4,
> Name4)
>
> I find this to be a lot less clutter than:
>
> class my_data
> {
> int Id;
> int Price;
> bool Sellable;
> int Shares;
> int Gain;
>
> // Boiler plate code...
> };
>
> Users of my_data can now access members as obj.get<my_data::Id>() The only
> thing in the above is that I can't declare the data to be protected so that
> users of my_data won't be able to do get<my_data::Price> = 0; for example.
> Ideally there should be a way so that some of the data members could be
> private to my_data and not exposed to even derived classes but I don't
> see a way for that. But protected could be handled through a slight
> modification in tuple:
>
> enum AccessSpecifier { Protected, Public }
>
> template< enum AccessSpecifier, typename... Args> class typle...
>
> template<typename.. Args> class tuple<Protected, Args...> // Make sure
> Args are declared protected
>
> With some specialization magic, we can let users define which arguments
> should be public (that is, the get<> method) and which should be protected.
> Does this make sense? I think we could add more magic so that users can
> only get and NOT set the values. Thoughts?
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk