5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++26でフォーマット文字列の{}に変数名を埋め込む

5
Last updated at Posted at 2026-05-10

C++26で追加される静的リフレクション(cpprefjp)を使うと、フォーマット文字列の波カッコの中に変数名を埋め込むような書き方ができないこともないです。

void f(int x, double y, const std::string& z) {
    println("{x}, {y:.2f}, {z:?}");  // 42, 3.14, "Hello"
}

int main() {
    f(42, 3.14159, "Hello");
}

↓実装はこちら

変数名から変数への変換は、変数のリフレクション値のリストから変数名が一致するものを探索することによりできます。
しかし、ローカル変数を含めたすべての変数のリストを取得する方法は現状ありません。上の実装では、 parameters_of() により関数の引数のリストを取得し、その中からのみ探索しています。

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?