auto specifier (C++11およびそれ以降)
提供: cppreference.com
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
宣言された変数の型が自動的に初期化子から推定されることを指定します。関数の場合、戻り値の型は末尾の戻り値の型であることを指定します.
Original:
Specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a 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.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集] 構文
auto variable initializer (C++11およびそれ以降) | |||||||||
auto function -> return type (C++11およびそれ以降) | |||||||||
[編集] 説明
1)ブロックスコープの変数を宣言する場合は、名前空間スコープで、forループなどのinitステートメントでは、変数の型は省略してもよいとキーワードautoを代わりに使用することができ.
Original:
When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword auto may be used instead.
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.
初期化子の型が決定されると、コンパイラは、関数呼び出しからのテンプレート引数推論のためのルールを使用しているかのようにキーワードautoを交換するタイプを決定します。キーワードautoは、型推論に参加するconstまたは&として、修正を伴うことがあります。関数呼び出しconst auto& i = expr;がコンパイルされている場合たとえば、
2) i
与え、u
の型は正確に架空のテンプレートtemplate<class U> void f(const U& u)で引数f(expr)のタイプです.Original:
Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto as if using the rules for template argument deduction from a function call. The keyword auto may be accompanied by modifies, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type of
i
is exactly the type of the argument u
in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled.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.
関数の宣言では、キーワードautoは自動タイプの検出を実行しません。それが唯一の末尾戻り値の型の構文の一部として機能.
Original:
In a 関数の宣言, the keyword auto does not perform automatic type detection. It only serves as a part of the trailing return type syntax.
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.
[編集] ノート
まで、C + +11、auto貯蔵期間指定子の意味を持っていた.
Original:
Until C++11, auto had the semantic of a 貯蔵期間指定子.
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.
[編集] 例
このコードを実行します
#include <iostream> #include <cmath> #include <typeinfo> template<class T, class U> auto add(T t, U u) -> decltype(t + u) // the return type of add is the type of operator+(T,U) { return t + u; } auto get_fun(int arg)->double(*)(double) // same as double (*get_fun(int))(double) { switch (arg) { case 1: return std::fabs; case 2: return std::sin; default: return std::cos; } } int main() { auto a = 1 + 2; std::cout << "type of a: " << typeid(a).name() << '\n'; auto b = add(1, 1.2); std::cout << "type of b: " << typeid(b).name() << '\n'; //auto int c; //compile-time error auto d = {1, 2}; std::cout << "type of d: " << typeid(d).name() << '\n'; auto my_lambda = [](int x) { return x + 3; }; std::cout << "my_lambda: " << my_lambda(5) << '\n'; auto my_fun = get_fun(2); std::cout << "type of my_fun: " << typeid(my_fun).name() << '\n'; std::cout << "my_fun: " << my_fun(3) << '\n'; }
出力:
type of a: int type of b: double type of d: std::initializer_list<int> my_lambda: 8 type of my_fun: double (*)(double) my_fun: 0.14112