Boost logo

Boost :

Subject: Re: [boost] PCL - Portable C++ Library
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2012-02-12 10:43:39


On 02/11/2012 05:53 PM, paul Fultz wrote:
>
>
>> All right so you still need the switch regardless in this case if you
>> want to implement a property(i) function.
>>
>> i.e.
>>
>> switch(i)
>> {
>> case 0:
>> return metadata<Self, 0>(self).get();
>>
>> case 1:
>> return metadata>self, 1>(self).get();
>>
>> ...
>> }
>
> Yes, the switch would still be needed to do the property(i), but I think the
> visitor pattern is the better way to go(especially once we have polymorphic
> lambdas in C++).

Well, you end up doing compile-time reflection.
Sure, it's better in a way since you can properly support any type for
any property without going to the string common denominator, but Qt
needs the ability to iterate over the properties of an object without
knowing what the type of that object is.

>> There are template-based techniques to have counters as well, but
>> they're quite tricky and tend to be fragile with subpar compilers.
>
> I'm curios as to what template techniques there are to do this. Please
> elaborate.

There are several, I have yet to find one that is really perfect.

The best there is is probably using overloading.
int_<0> counter(depth_<0>);

where depth_ is
template<int N> struct depth_ : depth_<N-1> {};
template<> struct depth_<0> {};

to read
#define READ identity<decltype(counter(depth_<MAX>()))>::type::value

and to increment,
int_<READ+1> counter(depth_<READ+1>);

The problems (apart from the fact that it depends on a decltype- or
typeof-like feature in the compiler and that it is all in all quite
hacky) are the interactions with ADL in certain template contexts,
especially with compilers that don't do two-phase lookup correctly (e.g.
MSVC).

I don't really see that kind of thing being democratised in any case.
You should find a way to avoid relying on compile-time counters.


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