Function template

来自cppreference.com

 
 
C + +語言
大會的主題
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
條件執行語句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代語句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
跳轉語句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函數聲明
lambda函數的聲明
函數模板
的歷史。內嵌說明
異常規範 (過時了)
noexcept說明 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
規範
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存儲時間說明符
constexpr說明 (C++11)
汽車符 (C++11)
alignas說明 (C++11)
初始化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
布爾文字
nullptr (C++11)
用戶定義的 (C++11)
表達式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
另一種表示形式
實用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
聲明類型別名 (C++11)
屬性 (C++11)
施放
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
隱式轉換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-風格和功能轉換
內存分配
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類特定的功能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
虛函數
覆蓋說明 (C++11)
最後說明 (C++11)
明確的 (C++11)
靜態的
特殊的成員函數
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
模板
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類模板
函數模板
模板特化
參數包 (C++11)
雜項
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
內聯彙編
 

目錄

[编辑] 說明

模板允許通用的功能設計,對不同類型的工作,而不需要重寫多次
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.

[编辑] 語法

[编辑] 宣言

template < template_arguments > function_declaration (1)
export template < template_arguments > function_declaration (2) (至 C++11)
#模板函數聲明引用錯誤:沒有找到與</ref>對應的<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.
#導出模板函數的聲明。這是非常罕見的編譯器上實現的,不應該使用的函數體可以被定義在一個單獨的文件引用錯誤:沒有找到與</ref>對應的<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.

[编辑] 參數

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 起)
1-2)
在函數內部identifier可以用來作為一種類型
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.
3)
在函數內部identifier可以用來作為一個常數
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.
4-6)
默認參數
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.

[编辑] 專業化

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.

[编辑] 呼叫

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.
#隱式模板參數,推導出函數的參數。可以毫不含糊
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.

[编辑] 為例

[edit]
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
}


[编辑] 見也

[编辑] 注釋

Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.