Boost logo

Boost :

Subject: Re: [boost] Workaround for compiler bugs
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2010-12-09 05:35:13


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 :-(

#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

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

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