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 — ラムダ式のキャプチャとして[=, this]を許可する [P0409R2] - cpprefjp C++日本語リファレンス

  1. 言語機能
  2. C++20
  3. ラムダ式のキャプチャとして[=, this]を許可する [P0409R2]

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

履歴 編集

ラムダ式のキャプチャとして[=, this]を許可する [P0409R2](C++20)

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

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

概要

C++17まで、ラムダ式のキャプチャとして[=, this]は指定できなかった。これは、デフォルトコピーキャプチャ (=) に、thisポインタのコピーキャプチャが含まれるため、重複指定はできないというものである。

C++20では、[=, this]のキャプチャ指定を許可する。これは、意味としては[=]と等価である。明示的にthisポインタをキャプチャして使用するという表明によって、可読性を向上させる目的に使える。

例

#include <iostream>

struct X {
  void f()
  {
    int value = 3;
    auto x = [=, this] { // [=]キャプチャと等価
      return value + g();
    };
    std::cout << x() << std::endl;
  }

  int g() const
  {
    return 2;
  }
};

int main()
{
  X().f();
}

関連項目

  • C++11 ラムダ式
  • C++20 [=]によるthisの暗黙のキャプチャを非推奨化

参照

  • P0409R2 Allow lambda capture [=, this]
You are visitor 01402520 — 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.