Boost logo

Boost :

From: Mat Marcus (mmarcus_at_[hidden])
Date: 2000-10-02 12:51:24


At 7:56 AM -0400 10/2/00, David Abrahams wrote:
>
>
>----- Original Message -----
>From: "Mat Marcus" <mmarcus_at_[hidden]>
>
>> Au contraire. I believe this is a key observation. Thanks! We hadn't
>> solved ISREF yet. In our work we've been using an ISCONST
>> metafunction that uses IsNonConstPointer for a discriminator. That is
>> (roughly)
>>
>> char IsNonConstPointer(void*);
>> int IsNonConstPointer(...);
>>
>> template <class T>
>> struct ISCONST {
>> enum {RET = (sizeof(IsNonConstPointer((T*)0)) == 1)}; // The pointer
>trick
>> };
>>
>> If we combine this with your idea we could try
>>
> > template <class T>
>> struct ISREF {
>> enum {RET = sizeof(IsNonConstPointer(&((Holder<T>*)0)->x) == 1)};
>> };
>>
>
>:( :( :(
>
>I don't think this will work. And VC6.3 bears me out:
>
>char IsNonConstPointer(void*);
>int IsNonConstPointer(...);
>
>template <class T> struct Holder { T x; Holder(); };
>
>template <class T>
>struct ISCONST {
> enum {RET = (sizeof(IsNonConstPointer((T*)0)) == 1)}; // The pointer
>trick
>};
>
>
>template <class T>
>struct ISREF {
> enum {RET = sizeof(IsNonConstPointer(&((Holder<T>*)0)->x) == 1)};
>};
>
>char a[ISREF<int&>::RET];
>char b[!ISREF<int>::RET]; // fail
>char c[!ISREF<int*>::RET]; // fail
>char d[!ISREF<int* const>::RET]; // fail
>char e[ISREF<const int&>::RET];
>char f[!ISREF<const int>::RET]; // fail
>char g[!ISREF<const int*>::RET]; // fail
>char h[!ISREF<const int* const>::RET]; // fail
>char i[ISREF<const volatile int&>::RET];
>char j[!ISREF<const volatile int>::RET]; // fail
>char k[!ISREF<const volatile int*>::RET]; // fail
>char l[!ISREF<const volatile int* const>::RET]; // fail
>
>Basically, it looks like ISREF<T>::RET is always true.
>
> > I can't wait to go to work to try this on my Windows compiler. (Did I
>> just say that? :-| ). Too bad Metrowerks' sizeof operator doesn't
>> work.
>
>What's wrong with sizeof()? Anyway, MW has partial specialization so you
>shouldn't need such hackery.
>
>-Dave

Monday morning, back at my Windows box. The above code seems to work
for me on VC6.3 after correcting some typos:

template <class T>
struct ISCONST {
   enum {RET = (1 != sizeof(&((Holder<T>*)0)->x))};
};

Cool. One aspect of this version of ISREF is that there are no
compiler warnings from VC6.3.

- Mat


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