名前空間
変種
操作

Declaring functions

提供: cppreference.com
< cpp‎ | language

 
 
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.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
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.
union型
関数型
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.
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)
静的
特殊なメンバ関数
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:
A function declaration introduces the function name and its type. It may appear in any scope, and is commonly placed in header files.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret name ( params ) ; (1)
ret name ( params ) cv ref except attr ; (2)
auto name ( params ) cv ref except attr -> ret ; (3) (C + + 11以来)
関数定義は、関数の本体を提供しています。それだけで名前空間やクラスのスコープに表示されることがあります.
Original:
A function definition provides the body of a function. It may only appear in namespace or class scope.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decl name ( params ) { body } (4)
attr decl name ( params ) cv ref except attr virt try init-list { body } catch (5)
attr decl name ( params ) cv ref except attr -> ret virt try init-list { body } catch (6) (C + + 11以来)
attr decl name ( params ) cv ref except attr virt = 0 ; (7)
attr decl name ( params ) cv ref except attr virt = default ; (8) (C + + 11以来)
attr decl name ( params ) cv ref except attr virt = delete ; (9) (C + + 11以来)

[編集] 説明

1)
典型的な関数の宣言
Original:
Typical function declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
包括的な関数の宣言
Original:
Comprehensive function declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
末尾の戻り値の型を持つ包括的な関数の宣言
Original:
Comprehensive function declaration with trailing return type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
典型的な非メンバ関数定義
Original:
Typical non-member function definition
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
包括的な関数定義
Original:
Comprehensive function definition
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
末尾の戻り値の型を持つ包括的な関数定義
Original:
Comprehensive function definition with trailing return type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
純粋仮想メンバー関数
Original:
Pure virtual member function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
8)
ディフォルトのメンバ関数
Original:
Defaulted member function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
9)
メンバ関数を削除してしまいました
Original:
Deleted member function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
attr(C++11) -
そのような関数の属性または[[noreturn]]として[[carries_dependency]]、任意の数の任意のシーケンス。関数名の前と後の両方で表示されることがあります
Original:
Optional sequence of any number of 関数の属性, such as [[noreturn]] or [[carries_dependency]]. May appear both before and after the function name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret -
関数は何も返さない場合に関数から返される型は、voidかもしれません。などへのポインタまたは参照することができますが、配列または関数型にすることはできません。戻り値の型を提供してはならないコンストラクタ、デストラクタ、および変換演算子を除いて、すべての機能に必要な.
Original:
the type returned by the function, may be void if the function returns nothing. Cannot be array or function type, although can be a pointer or reference to such. Required for all functions except constructors, destructors, and conversion operators, which must not provide a return type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decl -
noneまたは次のキーワードの一部で構成されています宣言指定子のシーケンス:静的は、externインラインvirtualexplicitfriendconstexprの、戻り値の型と組み合わせて、ret
Original:
declaration specifier sequence, which consists of none or some of the following keywords: 静的は、extern, インライン, virtual, explicit, friend, constexprの, combined with the return type, ret
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

{{par | cv |{{tr| オプションconstvolatile、またはconst volatile、非静的メンバ関数にのみ適用。クラスT、[[CPP /言語/ thisの型のメンバ関数である| Optional const, volatile, or const volatile, only applicable to non-static member functions. For a member function of class T, the type of the [[cpp/language/this}}|this pointer]] will be const T*, volatile T*, or const volatile T* respectively. A member function declared const cannot modify members of *this.}}

ref(C++11) -
オプション&または&&、コンストラクタやデストラクタ以外の非静的メンバ関数にのみ適用。クラスTのメンバ関数については、オーバーロードの解決を目的とした暗黙のオブジェクトパラメータの型がT&またはT&&それぞれ次のようになります。&&修飾メンバー関数だけ右辺値オブジェクト式で呼び出すことができます
Original:
Optional & or &&, only applicable to non-static member functions other than constructors or destructors. For a member function of class T, the type of the implicit object parameter for the purpose of overload resolution will be T& or T&& respectively: a &&-qualified member function can only be called on an rvalue object expression
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
except -
どちら動的例外仕様またはnoexcept仕様
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
virt(C++11) -
オプションoverrideまたはfinal、非静的メンバ関数にのみ適用
Original:
Optional override or final, only applicable to non-static member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
->ret(C++11) -
戻り値の型を引きずって、場合にのみ適用retですauto。タイプは、template <class T, class U> auto add(T t, U u) -> decltype(t + u);として引数名に依存したり、auto fpif(int)->int(*)(int)のように、複雑である場合に有用
Original:
Trailing return type, only applicable if ret is auto. Useful if the type depends on argument names, such as template <class T, class U> auto add(T t, U u) -> decltype(t + u); or is complicated, such as in auto fpif(int)->int(*)(int)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
init-list -
コンストラクタ初期化子リスト、コンストラクタ内でのみ使用
Original:
コンストラクタ初期化子リスト, only used in constructors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
try -
オプションの関数tryブロック。存在する場合、catchを提供しなければなりません
Original:
Optional function try block. If present, catch must be provided
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
catch -
-catchブロックをののみ適用tryのオプションのシーケンスが使用され.
Original:
Optional sequence of -catchブロックを, only applicable of try is used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
body -
関数の本体は、(ことによると空)の複合文
Original:
The body of the function, a (possibly empty) compound statement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
params -
パラメータのリスト
Original:
The list of parameters
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


[編集] 例1:非メンバ関数

#include <iostream>
#include <string>
 
// declaration in namespace(file) scope
// (the definition is provided later)
int f1();
 
// simple function with a default argument, returning nothing
void f0(const std::string& arg = "world") {
    std::cout << "Hello, " << arg << '\n';
}
 
// function returning a pointer to f0
auto fp11() -> void(*)(const std::string&) {
    return f0;
}
 
// function returning a pointer to f0, pre-C++11 style
void (*fp03())(const std::string&) {
    return f0;
}
 
int main()
{
    f0();
    fp11()("test");
    fp03()("again");
    int f2(std::string); // declaration in function scope
    std::cout << f2("bad12") << '\n';
}
 
// simple non-member function returning int
int f1() {
    return 42;
}
 
// function with an exception specification and a function try block
int f2(std::string str) noexcept try { 
    return std::stoi(str);
} catch(const std::exception& e) {
    std::cerr << "stoi() failed!\n";
    return 0;
}

Output:

Hello, world
Hello, test
Hello, again
stoi() failed!
0


[編集] 例2:メンバ関数

#include <iostream>
#include <string>
#include <utility>
#include <exception>
 
struct S {
    int data;
 
    // simple converting constructor (declaration)
    S(int val);
 
    // simple explicit constructor (declaration)
    explicit S(std::string str);
 
    // const member function (definition)
    virtual int getData() const { return data; }
 
};
 
// definition of the constructor
S::S(int val) : data(val) {
    std::cout << "ctor1 called, data = " << data << '\n';
}
 
// this constructor has a catch clause
S::S(std::string str) try : data(std::stoi(str)) {
    std::cout << "ctor2 called, data = " << data << '\n';
} catch(const std::exception&) {
    std::cout << "ctor2 failed, string was '" << str << "'\n";
    throw; // ctor's catch clause should always rethrow
}
 
struct D : S {
    int data2;
    // constructor with a default argument
    D(int v1, int v2 = 11) : S(v1), data2(v2) {}
 
    // virtual member function
    int getData() const override { return data*data2; }
 
    // lvalue-only assignment operator
    D& operator=(D other) & {
        std::swap(other.data, data);
        std::swap(other.data2, data2);
        return *this;
    }
};
 
int main()
{
    D d1 = 1;
    S s2("2");
    try {
         S s3("not a number");
    } catch(const std::exception&) {}
    std::cout << s2.getData() << '\n';
 
   D d2(3, 4);
   d2 = d1; // OK: assignment to lvalue
//   D(5) = d1; // ERROR: no suitable overload of operator=
}

Output:

ctor1 called, data = 1
ctor2 called, data = 2
ctor2 failed, string was 'not a number'
2
ctor1 called, data = 3