Search code examples
c++templatescompile-timestatic-assert

Static assert whether or not add operation is available


I'm trying to make a static assert that checks whether type A can be added to type B. Is there any way to check whether template type A is addable to template type B at compile time? A and B could really be anything at all.


Solution

  • With is_detected, you may do something like:

    template <typename LHS, typename RHS>
    using sum_t = decltype(std::declval<LHS>() + std::declval<RHS>());
    
    template <typename LHS, typename RHS> 
    using has_sum = is_detected<sum_t, LHS, RHS>;