
On 19/02/11 06:51, Frédéric Bron wrote:
Is it possible to determine at compile-time if a type has subtraction semantics? E.g. in struct A<T> I would like to decide at compile-time not to execute&A::subtract in the register_fns method. Then the subtract method is not instantiated and there is no error.
You can look at the code that will be reviewed soon for an extension of type traits.
#include<boost/type_traits/has_operator_minus.hpp>
boost::has_operator_minus<T, T, T>::value is what you want: is true if x-y returns something convertible to T is false if operator-(T, T) does not exist.
http://www.boostpro.com/vault/index.php?action=downloadfile&filename=type_tr...
Is there any way to do use the type traits to conditionally compile some code using some brief syntax? I.e. it would be nice if something like the following worked: template< typename T > void g( T x, T y ) { if( boost::has_operator_minus<T, T, T>::value ) { return x - y; } else { return T(); } } but of course gcc 4.4.3 tries to compile the x-y regardless of whether it can ever be executed. Or do you always need to specialise some struct or function elsewhere and then call a method on that struct or execute the function? Thanks, John.