is_reference current implamentation (why so complex?)

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_typetrai... 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

Dmitry Sychov wrote:
void main() {
This line is incorrect. main() returns int.
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)
The original code supports broken compilers.
Is it just because on some platforms sizeof(char) != 1 ?
sizeof(char) is 1 by definition.
If yes we can change 'char' to object with size >= sizeof(T&) to avoid this.
Types other than char (e.g. int or long) can also have size 1. Regards, m Send instant messages to your online friends http://au.messenger.yahoo.com

Dmitry Sychov wrote:
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.
There's no such platform that conforms to the C/C++ standards. sizeof(char) is defined to be 1, and all other types are measured in terms of this. The actual size of a char is given by CHAR_BITS. Sebastian Redl

On my VC 8.0 I've successfully rewrote "is_reference" implementation (from is_reference.hpp)
down to the following:
The code you're looking at is used for *broken compiler only*. Please google back through the mailing list to find out why it was done that way (basically "it worked"). For conforming compilers it's all handled in the two lines of code: BOOST_TT_AUX_BOOL_TRAIT_DEF1(is_reference,T,false) BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(typename T,is_reference,T&,true) Which uses partial specialisation.
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)
No sizeof(T&) can be 1, for example sizeof(char&) is 1. John.
participants (4)
-
Dmitry Sychov
-
John Maddock
-
Martin Wille
-
Sebastian Redl