c++ - Restriction of access to function -
this question has answer here:
i have generic class function want restrict instances of floating point types only, @ compile time. shown in example below:
template <typename t> class classname { // instance variables, etc.. void some_method() { // stuff, floating point types } }
how make compiler reject usage of some_method classname of non-floating point types?
i have been looking @ sfinae can't work, after several hours of failing i'm asking help.
thanks :)
you can use combination of std::is_floating_point
, std::enable_if
enable function floating point types:
#include <type_traits> template <typename t> class classname { // instance variables, etc.. public: template<typename t2 = t, typename = typename std::enable_if< std::is_floating_point<t2>::value >::type> void some_method() { // stuff, floating point types } }; int main() { classname<double> d; // ok d.some_method(); // ok classname<int> i; // ok i.some_method(); // error }
Comments
Post a Comment