GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — decltype(auto) - cpprefjp C++日本語リファレンス

  1. 言語機能
  2. C++14
  3. decltype(auto)

最終更新日時: 2025年07月10日 10時26分30秒 (JST)
Koichi Murase が更新

履歴 編集

decltype(auto)(C++14)

このページはC++14に採用された言語機能の変更を解説しています。

のちのC++規格でさらに変更される場合があるため関連項目を参照してください。

概要

decltype(auto)は、decltypeに与える式を右辺の式で置き換えて型推論する機能である。

int a = 3;
int b = 2;

decltype(a + b) c = a + b; // cの型はint
decltype(auto)  d = a + b; // dの型もint。autoが式「a + b」で置き換えられる

参照の変数をautoキーワードで型推論した場合はTとなるが、decltype(auto)で型推論した場合はT&となる。

int x = 3;
int& r = x;

auto           a = r; // aの型はint
decltype(r)    b = r; // bの型はint&
decltype(auto) c = r; // cの型はint&

この機能は、通常関数の戻り値型推論の戻り値型プレースホルダーとしても使用できる:

// autoの場合はintが戻り値型となるが、
// decltype(auto)とすることでint&が戻り値型となる。
decltype(auto) f(int& r)
{
  return r;
}

例

#include <type_traits>

int main()
{
  int x = 3;
  int& r = x;

  auto           a = r; // aの型はint
  decltype(r)    b = r; // bの型はint&
  decltype(auto) c = r; // cの型はint&

  static_assert(std::is_same<decltype(a), int>::value, "");
  static_assert(std::is_same<decltype(b), int&>::value, "");
  static_assert(std::is_same<decltype(c), int&>::value, "");
}

出力

この機能が必要になった背景・経緯

decltype(auto)は、C++14で導入された「通常関数の戻り値型推論」の機能で、参照の変数を参照のままreturn文で返せるようにするために導入された。

関連項目

  • C++14 通常関数の戻り値型推論
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.