显式对象参数(Deducing this,C++23)
什么是显式对象参数
显式对象参数(Explicit Object Parameter)是 C++23 引入的语言特性,也叫 "Deducing this"。它允许在成员函数的参数列表中用 this 关键字显式声明对象参数,而不是像以前那样隐式使用 this 指针。
关键的一点是:当显式对象参数使用 auto 或模板参数时,编译器会自动推导出调用者的实际类型——包括派生类类型。这让很多以前需要 CRTP(奇异递归模板模式)才能实现的静态多态,现在可以用更简洁的方式写出来。
struct Base {
// 传统方式:this 是隐式的
void foo() { /* this 是 Base* */ }
// C++23:显式对象参数
void bar(this Base& self) { /* self 就是 *this */ }
// C++23:推导类型——可以推导出派生类
void baz(this auto& self) { /* self 的类型由调用者决定 */ }
};基本示例
#include <iostream>
#include <string>
#include <type_traits>
// ============================
// 示例1:基本的显式对象参数
// ============================
struct Greeter {
std::string name;
// 显式对象参数——self 就是 *this
void greet(this const Greeter& self) {
std::cout << "你好,我是 " << self.name << std::endl;
}
// 可以用值传递
void greet_copy(this Greeter self) {
std::cout << "拷贝: " << self.name << std::endl;
}
};
void basic_demo() {
Greeter g{"张三"};
g.greet(); // self 绑定到 g
g.greet_copy(); // self 是 g 的拷贝
}
// ============================
// 示例2:自动推导 const/非 const
// ============================
struct Container {
int data[3] = {10, 20, 30};
// 一个函数同时处理 const 和非 const
// 传统方式需要写两个重载
auto& at(this auto& self, int index) {
return self.data[index];
}
};
void const_demo() {
Container c;
c.at(0) = 100; // 非 const,返回 int&
std::cout << "c.at(0) = " << c.at(0) << std::endl;
const Container cc;
// cc.at(0) = 100; // 编译错误:const int& 不能赋值
std::cout << "cc.at(0) = " << cc.at(0) << std::endl;
}
// ============================
// 示例3:替代 CRTP——静态多态
// ============================
// 传统 CRTP
/*
template<typename Derived>
struct BaseCRTP {
void interface() {
static_cast<Derived*>(this)->implementation();
}
};
struct DerivedCRTP : BaseCRTP<DerivedCRTP> {
void implementation() { std::cout << "CRTP 实现" << std::endl; }
};
*/
// C++23 方式——不需要模板参数
struct Base {
void interface(this auto& self) {
// self 会自动推导为派生类的类型
self.implementation();
}
void implementation() {
std::cout << "Base 默认实现" << std::endl;
}
};
struct Derived1 : Base {
void implementation() {
std::cout << "Derived1 实现" << std::endl;
}
};
struct Derived2 : Base {
void implementation() {
std::cout << "Derived2 实现" << std::endl;
}
};
struct Derived3 : Base {}; // 使用基类的默认实现
void crtp_demo() {
Derived1 d1;
Derived2 d2;
Derived3 d3;
d1.interface(); // "Derived1 实现"
d2.interface(); // "Derived2 实现"
d3.interface(); // "Base 默认实现"
}
// ============================
// 示例4:链式调用
// ============================
struct Builder {
int width_ = 0;
int height_ = 0;
std::string title_;
// 返回自身引用实现链式调用
// auto& 推导出实际类型,支持派生类链式调用
auto& set_width(this auto& self, int w) {
self.width_ = w;
return self;
}
auto& set_height(this auto& self, int h) {
self.height_ = h;
return self;
}
auto& set_title(this auto& self, std::string t) {
self.title_ = std::move(t);
return self;
}
void build(this const auto& self) {
std::cout << "构建: " << self.title_
<< " (" << self.width_ << "x" << self.height_ << ")"
<< std::endl;
}
};
void chain_demo() {
Builder()
.set_width(800)
.set_height(600)
.set_title("我的窗口")
.build();
}
// ============================
// 示例5:消除 const/非 const 重载
// ============================
// 传统方式:需要两个几乎相同的重载
struct OldStyle {
int value = 42;
int& get() { return value; }
const int& get() const { return value; }
};
// C++23:一个函数搞定
struct NewStyle {
int value = 42;
auto& get(this auto& self) {
return self.value;
}
};
void overload_demo() {
NewStyle ns;
ns.get() = 100; // 非 const
std::cout << "修改后: " << ns.get() << std::endl;
const NewStyle cns;
std::cout << "const: " << cns.get() << std::endl;
}
// ============================
// 示例6:按值传递实现递归 lambda
// ============================
void recursive_lambda_demo() {
// C++23 之前,递归 lambda 很困难
// C++23:用显式对象参数实现
auto factorial = [](this auto self, int n) -> int {
if (n <= 1) return 1;
return n * self(n - 1); // 递归调用自身
};
std::cout << "5! = " << factorial(5) << std::endl; // 120
std::cout << "10! = " << factorial(10) << std::endl; // 3628800
}
// ============================
// 示例7:右值引用限定
// ============================
struct Resource {
std::string data = "资源数据";
// 左值:返回引用
auto& get_data(this auto& self) {
return self.data;
}
// 右值:移动数据
auto get_data(this auto&& self)
requires std::is_rvalue_reference_v<decltype(self)>
{
return std::move(self.data);
}
};
void rvalue_demo() {
Resource r;
auto& ref = r.get_data(); // 左值,返回引用
std::cout << "引用: " << ref << std::endl;
auto moved = Resource{}.get_data(); // 右值,移动数据
std::cout << "移动: " << moved << std::endl;
}
int main() {
std::cout << "=== 基本用法 ===" << std::endl;
basic_demo();
std::cout << "\n=== const 推导 ===" << std::endl;
const_demo();
std::cout << "\n=== 替代 CRTP ===" << std::endl;
crtp_demo();
std::cout << "\n=== 链式调用 ===" << std::endl;
chain_demo();
std::cout << "\n=== 消除重载 ===" << std::endl;
overload_demo();
std::cout << "\n=== 递归 lambda ===" << std::endl;
recursive_lambda_demo();
std::cout << "\n=== 右值限定 ===" << std::endl;
rvalue_demo();
return 0;
}传统方式 vs Deducing this
| 场景 | 传统方式 | C++23 Deducing this |
|---|---|---|
| const/非 const 重载 | 写两个几乎相同的函数 | 一个 this auto& 搞定 |
| CRTP 静态多态 | template<typename D> class Base | 普通类 + this auto& |
| 链式调用返回派生类 | CRTP 或模板 | this auto& + 返回 self |
| 递归 lambda | std::function 或 Y-combinator | this auto self |
适用场景
- 消除 const/非 const 成员函数的重复
- 替代 CRTP 实现静态多态
- Builder 模式的链式调用(支持派生类)
- 递归 lambda
- 需要按值传递对象参数的场景
常见问题
显式对象参数的函数可以是 virtual 的吗?
不可以。带显式对象参数的成员函数不能是 virtual、static 的,也不能有 cv 限定符或引用限定符。
显式对象参数可以用任意名称吗?
可以。this auto& self、this auto& me、this const Base& obj 都可以。this 关键字只是标记它是对象参数,后面的名称随意。
this auto& 和模板有什么关系?
this auto& 本质上让成员函数变成了函数模板。编译器会为每个不同的调用类型生成一个实例。
可以同时有隐式 this 和显式对象参数吗?
不可以。一个类中,同名函数要么全用隐式 this,要么全用显式对象参数,不能混用。
注意事项
- C++23 特性,需要
-std=c++23 - 不能是 virtual 或 static
- 不能有 cv 限定符或引用限定符(这些由参数类型决定)
- 本质上是函数模板,可能导致代码膨胀
- 显式对象参数必须是第一个参数
- GCC 14+、Clang 18+、MSVC 2022 17.2+ 支持
总结
显式对象参数(Deducing this)是 C++23 最重要的语言特性之一。它允许在成员函数参数列表中用 this 关键字显式声明对象参数。当使用 this auto& 时,编译器会推导出调用者的实际类型,包括派生类。这带来了几个实际的好处:消除了 const/非 const 成员函数的重复代码,用简洁的方式替代了 CRTP 模式,使得 Builder 链式调用可以正确返回派生类类型,还让递归 lambda 成为可能。不过要注意,带显式对象参数的函数不能是 virtual 或 static 的。