Skip to content

败犬日报 2025-03-05

1. std::function 的签名和内部存储的函数前面可以不同

cpp
bool fn1(int) { /* ... */ }
std::function<long(bool)> fn2 = fn1;
auto ret = fn2(true);

这里 fn2 调用内部存储的 fn1 时会发生 bool 转 int,然后返回时 fn1 返回的 bool 又会转成 long。

此时可以通过 fn2.target<> 获取存储的函数(前提是知道类型),和 std::any_cast<> 类似。类型不对会得到空指针。

2. &&label 扩展语法

https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Labels-as-Values.html

cpp
#include <iostream>
int main() {
    a:
    auto x = &&a;
    std::cout << x << "\n";
}