Boost logo

Boost :

From: Dmitry Sychov (accelware_at_[hidden])
Date: 2006-04-29 04:32:08


Hello,

On my VC 8.0 I've successfully rewrote "is_reference"
implementation (from is_reference.hpp)
http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_reference

down to the following:

////////////////////////////////////////////////////////////////////

// IMPLEMENTATION

template <class T> struct _wrapper {};

template <class T> T& is_ref_fn(_wrapper<T>);
char is_ref_fn(...);

template <class T> struct _is_ref {
  static bool const value = sizeof is_ref_fn( _wrapper<T>()) == 1; };

// USAGE
void main() {

  bool is_ref_false = _is_ref<int>::value;
  bool is_ref_true = _is_ref<int&>::value;
}

////////////////////////////////////////////////////////////////////

I understand original code - but it uses
"is_reference_helper1" and "is_reference_helper2" for the same
thing - ie to distingwish between fn pointer size and char size but
this could be done in "one pass" (see my is_ref_fn(..) above)

also why to compare against fn ptr type and char type when we
can compare against 'T' type reference (sizeof(T&) >= 4) and
char (sizeof(char) == 1) (see my code)

Is it just because on some platforms sizeof(char) != 1 ?
If yes we can change 'char' to object with size >= sizeof(T&)
to avoid this.

Please comment on this.

Thanks,

Dmitry


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