Шаблоны: Специализации шаблона функции: Вывод аргумента шаблона: -> Кто-нибудь может рассказать еще несколько примеров для этого утверждения? - PullRequest
0 голосов
/ 22 октября 2010

Это утверждение из стандарта ISO C ++ 14.8.2.4 / 3rd: Вывод аргументов шаблона из типа

  A given type P can be composed from a number of other types, templates, 
  and non-type values:

    — A function type includes the types of each of the function parameters 
      and the return type.
    — A pointer to member type includes the type of the class object pointed 
      to and the type of the member pointed to.
    — A type that is a specialization of a class template (e.g., A<int>) 
      includes the types, templates, and non-type values referenced by the  
      template argument list of the specialization.
    — An array type includes the array element type and the value of the  
      array bound.

               In most cases, the types, templates, and non-type values that 
 are used to compose P participate in template argument deduction. That is,they
 may be used to determine the value of a template argument, and the value so 
 determined must be consistent with the values determined elsewhere. In certain  
 contexts, how ever, the value does not participate in type deduction, but 
 instead uses the values of template arguments that  were either deduced 
 elsewhere or explicitly specified. If a template parameter is used only in 
 nondeduced contexts and is not explicitly specified, template argument 
 deduction fails.

Привет всем, я пытался вывести аргументы шаблона, беря адрес функции шаблон, а также аргументы шаблона функции преобразования Deducing.

В 4-м пункте ... я знаю, что компилятор не может определить значение границы основного массива, если граница не ссылается на ссылку или тип указателя. Границы основных массивов не являются частью типов параметров функции.

Может ли кто-нибудь в состоянии объяснить каждый пункт примером, пожалуйста ...

Ответы [ 2 ]

1 голос
/ 22 октября 2010

Не уверен, что вы спрашиваете, но в отношении "Компилятор не может определить значение привязки основного массива, если граница не ссылается на ссылку или тип указателя", я думаю, что там должна быть какая-то опечатка.

Канонический пример вывода параметра шаблона из типа массива, включая bound,:

typedef ptrdiff_t Size;    // For example.

template< class Type, Size n >
Size countOf( Type (&)[n] ) { return n; }

, где Type и n выводятся из массива, предоставленного в качестве фактического аргумента

Ура & hth.,

0 голосов
/ 22 октября 2010

Некоторые примеры:

#include <string.h>
#include <ctype.h>
#include <utility>
#include <vector>

template <class R, class F, class S>
void function_pointer( R (*func)(F, S) );

template <class T, class Object>
void member_pointer( T Object::*);

template <class T, class U, template <class> class Alloc>
void a_vector(const std::vector<T, Alloc<U> >&);

template <class T, std::size_t N>
void array_ref(T (&)[N]);  //arrays decay to pointer when passed by value

template <class T, class U>
void types_must_agree(std::pair<T, U>, T (*) (U));

template <class T>
void non_deduced_context(typename std::pair<T, T>::first_type);

template <class T>
void non_deduced_context_deduced_from_another_argument(typename std::pair<T, T>::first_type, T);

int main()
{
    function_pointer(&strcmp);
    member_pointer(&std::pair<int, double>::first);
    a_vector(std::vector<int>());
    char c[10];
    array_ref(c);
    types_must_agree(std::pair<int, int>(), &isspace);
    non_deduced_context<int>(10);
    non_deduced_context_deduced_from_another_argument(10, 20);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...