I want to implement a private function based on a boolean template parameter. Something like that:
#include <iostream>
using namespace std;
template <bool is_enabled = true>
class Aggregator {
public:
    void fun(int a) {
        funInternal(a);
    }
private:
    void funInternal(int a, typename std::enable_if<is_enabled>::type* = 0) {
        std::cout << "Feature is enabled!" << std::endl;
    }
    void funInternal(int a, typename std::enable_if<!is_enabled>::type* = 0) {
        std::cout << "Feature is disabled!" << std::endl;
    }
};
int main()
{
   Aggregator<true> a1;
   Aggregator<false> a2;
   a1.fun(5);
   a2.fun(5);
   return 0;
}
But the program above does not compile: error: no type named 'type' in 'struct std::enable_if' void funInternal(int a, typename std::enable_if::type* = 0).
Is it possible to realize the desired behavior with enable_if?
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.
The following is an adaptation of the solution (http://coliru.stacked-crooked.com/a/480dd15245cdbb6f) provided by @chris in the comments, which seems to meet your needs.
#include <iostream>
template<bool is_enabled = true>
class Aggregator
{
public:
    void fun(int a)
    {
        funInternal(a);
    }
private:
    template<bool enabled = is_enabled>
    void funInternal(typename std::enable_if<enabled, int>::type a)
    {
        std::cout << "Feature is enabled!" << std::endl;
    }
    template<bool enabled = is_enabled>
    void funInternal(typename std::enable_if<!enabled, int>::type a)
    {
        std::cout << "Feature is disabled!" << std::endl;
    }
};
int main()
{
    Aggregator<true> a1;
    Aggregator<false> a2;
    a1.fun(5);
    a2.fun(5);
    return 0;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With