Boost logo

Boost :

Subject: Re: [boost] Workaround for compiler bugs
From: Edward Diener (eldiener_at_[hidden])
Date: 2010-12-09 08:23:12


On 12/9/2010 5:35 AM, Anthony Williams wrote:
> Edward Diener<eldiener_at_[hidden]> writes:
>
>> I will continue to look for a workaround for VC9 and earlier, as I
>> think my library should be usable by VC9, VC8, and perhaps
>> VC7.1. Thanks for your neat solution for VC10 !
>
> The following works with VC8, VC10 and g++ 4.4. It causes an Internal
> Compiler Error with VC7.1 though :-(

Thanks, Anthony ! Works with VC9 also. I will play around more with
VC7.1 but I will not worry if I can not get it to work there as that is
a pretty old version of VC now. My original code works with gcc 4+ so
gcc is not an issue.

>
> #include<iostream>
>
> typedef char small_type;
>
> struct large_type
> {
> small_type dummy[2];
> };
>
>
> template<bool,typename T>
> struct enable_if;
>
> template<typename T>
> struct enable_if<true,T>
> {
> typedef T type;
> };
>
> template<typename T,typename DT>
> struct has_member_cMem_of_type
> {
> template<typename U>
> static small_type check2(DT (U::*));
> template<typename U>
> static large_type check2(U);
>
> template<typename U>
> static typename enable_if<
> sizeof(check2(&U::cMem))==sizeof(small_type),
> small_type>::type
> has_matching_member(int);
> template<typename U>
> static large_type has_matching_member(...);
>
> static const bool value=sizeof(has_matching_member<T>(0))==sizeof(small_type);
> };
>
>
> struct Gipf { };
>
> struct Dvonn {
> Gipf cMem;
> };
>
> struct Tzaar {
> int cMem;
> };
>
> struct Yinsh {
> int Zertz;
> void cMem();
> };
>
> struct Derived:
> Dvonn
> {};
>
>
> int main() {
> std::cout<<"one:"<<has_member_cMem_of_type<Dvonn, Gipf>::value<<std::endl;
>
> std::cout<<"two" ":"<<!has_member_cMem_of_type<Dvonn, int>::value<<std::endl;
>
> std::cout<<"three" ":"<<!has_member_cMem_of_type<Tzaar, Gipf>::value<<std::endl;
> std::cout<<"four" ":"<< has_member_cMem_of_type<Tzaar, int>::value<<std::endl;
>
> std::cout<<"five" ":"<<!has_member_cMem_of_type<Yinsh, Gipf>::value<<std::endl;
> std::cout<<"six" ":"<<!has_member_cMem_of_type<Yinsh, int>::value<<std::endl;
>
>
> std::cout<<"seven" ":"<<has_member_cMem_of_type<Derived, Gipf>::value<<std::endl;
> std::cout<<"eight" ":"<<!has_member_cMem_of_type<Derived, int>::value<<std::endl;
> }
>
> Anthony


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