Function template
Материал из cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Содержание |
[править] Описание
Шаблоны позволяют общего назначения, функции, которые работают на различных типов, без необходимости переписывать его несколько раз
Original:
Templates allow generic function design that work on various types, without the need of rewriting it multiple times
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Синтаксис
[править] Декларацию
template < template_arguments > function_declaration
|
(1) | ||||||||
export template < template_arguments > function_declaration
|
(2) | (до C++11) | |||||||
Шаблон # объявление функции Ошибка цитирования Отсутствует закрывающий тег </ref>
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
# Экспортируемых декларации шаблон функции. Тело функции может быть определен в отдельном файле Ошибка цитирования Отсутствует закрывающий тег </ref>
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Аргументы
| class identifier | (1) | ||||||||
| typename identifier | (2) | ||||||||
| integral_type identifier | (3) | ||||||||
class identifier = type_name
|
(4) | (начиная с C++11) | |||||||
typename identifier = type_name
|
(5) | (начиная с C++11) | |||||||
integral_type identifier = const_expr
|
(6) | (начиная с C++11) | |||||||
Внутри функции identifier может быть использован в качестве типа
3) Original:
Inside the function identifier can be used as a type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Внутри функции identifier может быть использован в качестве постоянной
4-6) Original:
Inside the function identifier can be used as a constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
По умолчанию аргументы
Original:
Default arguments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Специализация
| Этот раздел не завершён Причина: partial specialization |
template <> ret function_name < template_args > ( func_args ) body
|
|||||||||
Специализация меняет реализации шаблона функции для конкретных параметров шаблона
Original:
Specialization changes the implementation of the template function for specific template parameters
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Вызов
function_name < template_args > ( func_args )
|
(1) | ||||||||
function_name ( unambiguous_func_args )
|
(2) | ||||||||
# Явных аргументов шаблона, если func_args не совпадают совершенно с типами, как в шаблон декларации (с заданной template_args) обычное литье произойдет
Original:
# Explicit template arguments, if func_args don't match perfectly with the types as in the template declaration (with the given template_args) the usual casting will occur
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
# Неявных аргументов шаблона, выводится из аргументов функции. Нет неоднозначность может присутствовать
Original:
# Implicit template arguments, deduced from the function arguments. No ambiguity can be present
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
| Этот раздел не завершён Причина: better explanation/example |
[править] Пример
| Этот раздел не завершён |
template<typename T> struct S { template<typename U> void foo(){} }; template<typename T> void bar() { S<T>s; s.foo<T>(); // error: < parsed as less than operator s.template foo<T>(); // OK }
[править] См. также
[править] Notes
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.