C++11模板元編程-std::enable_if示例詳解


C++11模板元編程-std::enable_if示例詳解

 更新時間:2020年10月29日 09:24:06   作者:半杯茶的小酒杯  

C++11中引入了std::enable_if函數,函數原型如下:

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

可能的函數實現:

1
2
3
4
5
template < bool B, class T = void >
struct enable_if {};
  
template < class T>
struct enable_if< true , T> { typedef T type; };

由上可知,只有當第一個模板參數為true時,enable_if會包含一個type=T的公有成員,否則沒有該公有成員。

頭文件:

1
#include <type_traits>

std::enable_if使用場景

1、限制模板函數的參數類型

在某些場景下,我們需要實現只有特定類型可以調用的模板函數。如下代碼所示,通過對返回值使用std::enable_if和在模板參數中使用std::enable_if均實現了只允許整形參數調用函數的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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;
}

當使用float類型參數調用函數時,程序會報錯:

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

2. 模板類型偏特化

在使用模板編程時,可以利用std::enable_if的特性根據模板參數的不同特性進行不同的類型選擇。

如下所示,我們可以實現一個檢測變量是否為智能指針的實現:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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;
}

程序輸出:

not smart pointer
is smart pointer
is smart pointer

參考材料

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

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

總結

到此這篇關於C++11模板元編程-std::enable_if的文章就介紹到這了,更多相關C++11模板元編程-std::enable_if內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM