C++11 template metaprogramming std::enable_if example in detail

  • 2020-11-20 06:12:05
  • OfStack

The std::enable_if function is introduced in C++11. The function prototype is as follows:


template< bool B, class T = void >
struct enable_if;

Possible function implementations:


template<bool B, class T = void>
struct enable_if {};
 
template<class T>
struct enable_if<true, T> { typedef T type; };

As we can see above, enable_if contains a public member of type=T only if the first template parameter is true, otherwise there is no public member.

The header file:


#include <type_traits>

std::enable_if usage scenario

1. Limit the parameter types of template functions

In some scenarios, we need to implement template functions that only certain types can call. Using std::enable_if for the return value and std::enable_if for the template arguments, as shown in the following code, implements the ability to call functions only with plastic arguments.


// enable_if example: two ways of using enable_if
#include <iostream>
#include <type_traits>

// 1. the return type (bool) is only valid if T is an integral type:
template <class T>
typename std::enable_if<std::is_integral<T>::value,bool>::type
 is_odd (T i) {return bool(i%2);}

// 2. the second template argument is only valid if T is an integral type:
template < class T,
      class = typename std::enable_if<std::is_integral<T>::value>::type>
bool is_even (T i) {return !bool(i%2);}

int main() {

 short int i = 1;  // code does not compile if type of i is not integral

 std::cout << std::boolalpha;
 std::cout << "i is odd: " << is_odd(i) << std::endl;
 std::cout << "i is even: " << is_even(i) << std::endl;

 return 0;
}

When a function is called with an argument of type float, the program reports an error:

[

error: no matching function for call to 'is_odd(float & )'

]

2. Partial specialization of template type

When programming with templates, you can take advantage of the features of std::enable_if to make different type choices depending on the characteristics of the template parameters.

As shown below, we can realize the implementation of detecting whether a variable is a smart pointer:


#include <iostream>
#include <type_traits>
#include <memory>

template <typename T>
struct is_smart_pointer_helper : public std::false_type {};

template <typename T>
struct is_smart_pointer_helper<std::shared_ptr<T> > : public std::true_type {};

template <typename T>
struct is_smart_pointer_helper<std::unique_ptr<T> > : public std::true_type {};

template <typename T>
struct is_smart_pointer_helper<std::weak_ptr<T> > : public std::true_type {};

template <typename T>
struct is_smart_pointer : public is_smart_pointer_helper<typename std::remove_cv<T>::type> {};

template <typename T>
typename std::enable_if<is_smart_pointer<T>::value,void>::type check_smart_pointer(const T& t)
{
  std::cout << "is smart pointer" << std::endl;
}

template <typename T>
typename std::enable_if<!is_smart_pointer<T>::value,void>::type check_smart_pointer(const T& t)
{
  std::cout << "not smart pointer" << std::endl;
}

int main()
{
  int* p(new int(2));
  std::shared_ptr<int> pp(new int(2));
  std::unique_ptr<int> upp(new int(4));

  check_smart_pointer(p);
  check_smart_pointer(pp);
  check_smart_pointer(upp);
  
  return 0;
}

Program output:

[

not smart pointer
is smart pointer
is smart pointer

]

Reference material

http://www.cplusplus.com/reference/type_traits/enable_if/

https://en.cppreference.com/w/cpp/types/enable_if

conclusion


Related articles: