Создавайте шаблоны только для базовых типов данных - PullRequest
2 голосов
/ 31 октября 2019

Как мы можем заставить шаблон принимать только основные типы данных.

template <typename T> 
void GetMaxValue( T& x ) 
{ 
//... Finds max Value
} 

В приведенной выше функции GetMaxValue мы можем передать любое значениебез каких-либо ошибок.

Но стандартная функция std::numeric_limits<T>::max() справилась с этим. Например:

auto max = std::numeric_limits< std::map<int,int> >::max();

выдаст ошибку error C2440: '<function-style-cast>' : cannot convert from 'int' to 'std::map<_Kty,_Ty>'

1 Ответ

5 голосов
/ 31 октября 2019

С ограничениями в C ++ 20:

#include <type_traits>
template <class T> 
requires std::is_arithmetic_v<T>
void GetMaxValue( T& x ) 
{ 
//... Finds max Value
}

Использование:

int a = 0;
GetMaxValue(a); // fine

std::vector<int> b;
GetMaxValue(b); // compiler error

Демо


С std::enable_if в противном случае:

template <class T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0> 
void GetMaxValue( T& x ) 
{ 
//... Finds max Value
}

Демонстрация 2


Предварительные ограничения сообщений об ошибках труднее читать:

error: no matching function for call to 'GetMaxValue(std::vector<int>&)'
  |     GetMaxValue(b); // compiler error
  |                  ^
Note: candidate: 'template<class T, typename std::enable_if<is_arithmetic_v<T>, int>::type <anonymous> > void GetMaxValue(T&)'
  | void GetMaxValue( T& x )
  |      ^~~~~~~~~~~
note:   template argument deduction/substitution failed:
error: no type named 'type' in 'struct std::enable_if<false, int>'
  | template <class T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
  |                                                                     ^
In instantiation of 'void GetMaxValue(T&) [with T = int; typename std::enable_if<is_arithmetic_v<T>, int>::type <anonymous> = 0]'

против

error: cannot call function 'void GetMaxValue(T&) [with T = std::vector<int>]'
  |     GetMaxValue(b); // compiler error
  |                  ^
note: constraints not satisfied
In function 'void GetMaxValue(T&) [with T = std::vector<int>]':
    required by the constraints of 'template<class T>  requires  is_arithmetic_v<T> void GetMaxValue(T&)'
note: the expression 'is_arithmetic_v<T>' evaluated to 'false'
  | requires std::is_arithmetic_v<T>
  |          ~~~~~^~~~~~~~~~~~~~~~~~
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...