Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2006-03-31 13:26:48


"Andy Little" wrote
> "Tobias Schwinger" wrote
>
>> // IS_TYPE((int))::value is true
>> // IS_TYPE((1))::value is false
>>
>> Does anyone know how to implement this without a typeof operator?
>
> I havent got a solution but one avenue thats seems to be in the right field is
> to exploit the difference between a function call and a declaration:
>
> Sometype func_or_value( Entity_to_test);
>
> eg assume Entity_to_test is a type then func_or_value is a function, else
> func_or_value is a variable.

The following seems to work in VC7.1 though it declares a variable if T is a
value:

-------------

#include <boost/preprocessor/cat.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/type_traits/is_function.hpp>
#include <iostream>

// necessary for the return/decl type of VARIABLE_OR_TYPE(T)
struct SomeType{
    // SomeType must have a unconstrained value ctor
    template <typename T> SomeType( T const &){};
};

#define VARIABLE_OR_TYPE(T) \
SomeType BOOST_PP_CAT(var_or_func,T)(T);

// test
 VARIABLE_OR_TYPE(int);
 VARIABLE_OR_TYPE(1);

int main()
{
// these would need to be wrapped in the macro I guess...

    // var_or_funcint is the decl from VARIABLE_OR_TYPE(int);
   typedef BOOST_TYPEOF(&var_or_funcint) tt;
   std::cout << boost::is_function<boost::remove_pointer<tt>::type >::value
<<'\n';

     // var_or_func1 is the decl from VARIABLE_OR_TYPE(1);
   typedef BOOST_TYPEOF(&var_or_func1) t1;
   std::cout << boost::is_function<boost::remove_pointer<t1>::type >::value
<<'\n';

}

regards
Andy Little


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