次のインターフェースを持つオブジェクトがあるとします。
struct Node_t {
... const std::vector< something >& getChilds() const;
} node;
今、私は次のauto
ような変数でプロパティにアクセスします:
auto childs = node->getChilds();
の種類はchilds
何ですか? または1へのstd::vector< something >
参照?
のタイプはchilds
になりますstd::vector<something>
。
auto
テンプレートタイプの演繹と同じルールを利用しています。ここで選択されるタイプは、template <typename T> f(T t);
のような呼び出しで選択されるタイプと同じf(node->getChilds())
です。
同様に、auto&
によって選択されるのと同じタイプを取得しtemplate <typename T> f(T& t);
、によって選択されるauto&&
のと同じタイプを取得しtemplate <typename T> f(T&& t);
ます。
auto const&
同じことが、またはのような他のすべての組み合わせにも当てはまりますauto*
。
それはstd::vector<something>
です。参照が必要な場合は、次のようにすることができます。
auto & childs = node->getChilds();
もちろん、それは const 参照になります。
auto
を与えますstd::vector<something>
。参照修飾子を指定するauto &
か、代わりに次を使用できますdecltype
。
decltype( node->getChilds() ) childs = node->getChilds();