Boost logo

Boost :

From: Matus Chochlik (chochlik_at_[hidden])
Date: 2008-04-12 08:18:58


Hmm, just realized that the design of mine has one dumb flaw. It's not
working when the common base is not in the set:
 ...

struct A { };
struct B { };
struct C : A { };
struct D : B { };
struct E : B { };
struct F : B { };
struct G : F { };

...
// ok
print_result(cout, "[C,C,A] -> A", is_same<common_type<C, C,
A>::result, A>::value);
print_result(cout, "[B,D,E] -> B", is_same<common_type<B, D,
E>::result, B>::value);
print_result(cout, "[B,D,G] -> B", is_same<common_type<B, D,
G>::result, B>::value);
// nogood :/
print_result(cout, "[D,E,F] -> B", is_same<common_type<D, E,
F>::result, B>::value);

On Sat, Apr 12, 2008 at 12:41 PM, Matus Chochlik <chochlik_at_[hidden]> wrote:
>
> Hey,
>
> my implementation is in the attachment. It's rather ugly ;) so it's probably not what You had in mind, but AFAIK it works.
>
> the common_type<...> template is declared only for three args, but with a little help from boost/preprocessor it could be
> generalized to N params. Unfortunatelly i don't have the time to play with that right now :-P.
>
> Nice puzzler though ;)
>
> Best,
>
>
>
>
> On Sat, Apr 12, 2008 at 12:15 PM, JOAQUIN M. LOPEZ MUÑOZ <joaquin_at_[hidden]> wrote:
>
> > ________________________________________
> > De: boost-bounces_at_[hidden] [boost-bounces_at_[hidden]] En nombre de Eric Niebler [eric_at_[hidden]]
> > Enviado el: sábado, 12 de abril de 2008 9:28
> > Para: Boost mailing list
> > Asunto: [boost] A C++ puzzler
> >
> >
> > > I recently had a C++ problem and found an answer that tickled my brain.
> > > In the spirit of Car Talk on NPR, I thought I'd share it in the form of
> > > a puzzler.
> > [...]
> >
> > Hello Eric, I am not sure if this is what you have in mind, since from your description
> > is not clear how the input types are represented: I've assumed they're just the
> > args of a class template with a fixed number of template parameters:
> >
> > struct default_t{};
> >
> > template<typename T>
> > struct holder_base{typedef T type;};
> >
> > template<>
> > struct holder_base<default_t>{};
> >
> > template<typename T,int>
> > struct holder:virtual holder_base<T>{};
> >
> > template<typename T1,typename T2,typename T3,typename T4,typename T5>
> > struct common_type_base:
> > holder<T1,1>,holder<T2,2>,holder<T3,3>,holder<T4,4>,holder<T5,5>{};
> >
> > template<typename T1,typename T2,typename T3,typename T4,typename T5>
> > struct common_type:common_type_base<T1,T2,T3,T4,T5>
> > {
> > typedef typename common_type_base<T1,T2,T3,T4,T5>::type type;
> > };
> >
> > template<>
> > struct common_type<default_t,default_t,default_t,default_t,default_t>
> > {
> > typedef default_t type;
> > };
> >
> > Best,
> >
> > Joaquín M López Muñoz
> > Telefónica, Investigación y Desarrollo
> >
> >
> >
> > _______________________________________________
> > Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
> >
>
>
>
> --
> ________________
> ::matus_chochlik

-- 
________________
::matus_chochlik



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