Determine at compile time whether type has subtraction semantics?

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. template< typename T > struct A { void register_fns() { &A::subtract; } T subtract( T x, T y ) { return x - y; } }; void test() { A< int >().register_fns(); // int: Fine A< std::string >().register_fns(); // string: Compile error } I didn't see anything in Boost.TypeTraits Thanks, John.

AMDG On 2/18/2011 9:50 AM, John Reid 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.
template< typename T > struct A { void register_fns() { &A::subtract; }
T subtract( T x, T y ) { return x - y; } };
void test() { A< int >().register_fns(); // int: Fine A< std::string >().register_fns(); // string: Compile error }
I didn't see anything in Boost.TypeTraits
I believe the type traits extensions in the review queue handle this. In Christ, Steven Watanabe

On Fri, Feb 18, 2011 at 12:55 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 2/18/2011 9:50 AM, John Reid 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.
I believe the type traits extensions in the review queue handle this.
Just to be clear, you can only check syntax at compile-time, not semantics. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On 18/02/11 18:05, Dave Abrahams wrote:
On Fri, Feb 18, 2011 at 12:55 PM, Steven Watanabe<watanabesj@gmail.com> wrote:
AMDG
On 2/18/2011 9:50 AM, John Reid 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.
I believe the type traits extensions in the review queue handle this.
Thanks.
Just to be clear, you can only check syntax at compile-time, not semantics.
Fair point.

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... Frédéric

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...
This looks good but it isn't compiling for me right now. Just compiling this include: #include <boost/type_traits/has_operator_minus.hpp> gives me this error: In file included from /home/john/Dev/ThirdParty/boost/sandbox/type_traits/boost/type_traits/has_operator_minus.hpp:55, from src/sandbox.cpp:2: /home/john/Dev/ThirdParty/boost/sandbox/type_traits/boost/type_traits/detail/has_binary_operator.hpp:165: error: expected constructor, destructor, or type conversion before ‘( ’ token svn revision 69069, gcc 4.4.3

This looks good but it isn't compiling for me right now. Just compiling this include: #include <boost/type_traits/has_operator_minus.hpp>
gives me this error:
In file included from /home/john/Dev/ThirdParty/boost/sandbox/type_traits/boost/type_traits/has_operator_minus.hpp:55, from src/sandbox.cpp:2: /home/john/Dev/ThirdParty/boost/sandbox/type_traits/boost/type_traits/detail/has_binary_operator.hpp:165: error: expected constructor, destructor, or type conversion before ‘( ’ token
Probably the order of include directories is 1. boost, 2. sandbox. You need 1. sandbox, 2. boost. Can you check? Frédéric

On 21/02/11 14:14, Frédéric Bron wrote:
Probably the order of include directories is 1. boost, 2. sandbox. You need 1. sandbox, 2. boost. Can you check? Yup you're right, I probably could have thought of this myself. Now I need to get Boost.Build to include them in the right order.
Thanks, John.

On 21/02/11 14:14, Frédéric Bron wrote:
This looks good but it isn't compiling for me right now. Just compiling this include: #include<boost/type_traits/has_operator_minus.hpp>
gives me this error:
In file included from /home/john/Dev/ThirdParty/boost/sandbox/type_traits/boost/type_traits/has_operator_minus.hpp:55, from src/sandbox.cpp:2: /home/john/Dev/ThirdParty/boost/sandbox/type_traits/boost/type_traits/detail/has_binary_operator.hpp:165: error: expected constructor, destructor, or type conversion before ‘( ’ token
Probably the order of include directories is 1. boost, 2. sandbox. You need 1. sandbox, 2. boost.
OK I got this working with the Boost.Build advice here: http://lists.boost.org/boost-build/2011/02/24625.php Thanks, John.

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.
participants (4)
-
Dave Abrahams
-
Frédéric Bron
-
John Reid
-
Steven Watanabe