Search code examples
c++c++11traitstemplate-classes

Disable a function by throwing error at compile-time with template class using traits


I have a class, let's call it Foo with several methods:

template<typename T>
class Foo {
public:
   Foo()               { /* ... */ }
   bool do_something() { /* ... */ }

   // This method should be callable only if:
   // std::is_floating_point<T>::value == true
   void bar() { 
      // Do stuff that is impossible with integer
   }
};

I would like to be able to construct both Foo<double> and Foo<int> But I don't want to allow calls to bar() when the type T is not a floating point type. I also want the error to be generated at compile-time and not at the run-time. So, what I want is:

Foo<double> a;
a.bar();                        // OK
Foo<int> b;
bool res = b.do_something();    // OK
b.bar();                        // WRONG: compile error

I tried a lot of things with enable_if (with posts like this or this one) but I can't use anymore the int type with Foo. For example:

typename std::enable_if<std::is_floating_point<T>::value>::type
bar() { /* ... */ } 

main.cpp:112:28:   required from here
foo.h:336:5: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
 bar() {

How can I constrain the use of bar() to floating point types but allow integer type to use in other places?


Solution

  • void bar() { 
       static_assert(std::is_floating_point<T>::value,
                     "this method can only be called for floating-point Foos!");
       // do stuff
    }