On Wed, 28 Nov 2007 23:38:19 -0800 (PST), Aries Sun wrote:
The following code can be compiled without error:
typedef char True; // sizeof(True) == 1
typedef struct { char a[2]; } False; // sizeof(False) 1
//...
template <typename TTrue isPtr( T * );
template <typename TFalse isPtr(T);
#define is_ptr( e ) (sizeof(isPtr(e ))==sizeof(True ))
Note that normally you would do that like this:
#include <iostream>
struct true_type { enum { result = true }; };
struct false_type { enum { result = false }; };
template<typena me T>
struct is_pointer: public false_type
{
};
template<typena me T>
struct is_pointer<T*>: public true_type
{
};
int main()
{
std::cout << "char* : " << is_pointer<char *>::result << std::endl;
std::cout << "int : " << is_pointer<int> ::result << std::endl;
}
Because true_type and false_type do not have a common anchestor, you
can now also do something like this:
template<typena me T>
void handle_somethin g_do(const T& t, false_type); // for non-pointer cases
template<typena me T>
void handle_somethin g_do(const T& t, true_type); // for pointer cases
void handle_somethin g(const T& t)
{
handle_somethin g_do(t, is_pointer<T>() );
}
Especially doing a sizeof on a function's return value is somewhat
weird in my opinion. Does the function get called or not? If it
does, what does the function do? How should you implement it? Why
should it take room in the executable, when all that is done is
done at compile time?
--
Joel Yliluoma -
http://iki.fi/bisqwit/