败犬日报 2025-03-25
1. 游览器插件 dark reader
网页自动夜间模式,还能隐藏水印(因为它能强行均衡对比度,抹除纯色区域的所有信息)。
2. windows 快速启动程序
可以用 everything + powertoys run。
3. 设置编译器输出高亮
4. “重载决议的一个例子”的解决方法
前天日报内容:
cpp
#include <cstdio>
template <typename T>
void f(T &&x) {
printf("1\n");
}
void f(const int &x) { printf("2\n"); }
int main() {
int x = 1;
f(x);
}
在实践上如果有多个行为不一致的函数,怎么匹配想要的函数?
可以加 requires(但不一定是好的实践)。标准库的做法是换函数名,无法换函数名(比如构造函数),在函数形参开头加一个不同的占位参数(比如 std::optional
构造函数的 std::in_place_t
,std::set
构造函数的 std::from_range_t
)。
5. C++ 解决三方库的各种链接问题,还有各种 libstdc++、compilerrt 之类的问题,真的很傻比
rt。
6. deducing this + CRTP 的隐藏坑
给某个类注入一个函数,然后这个类被别人继承,再通过派生类调用注入的函数,这时 self 是别人派生的那个类。
cpp
#include <iostream>
template <typename Derived>
struct Base {
void foo(this auto&& self) {
std::cout << "self is " << typeid(decltype(self)).name() << "\n";
}
};
struct Derived : Base<Derived> {};
struct FurtherDerived : Derived {};
int main() {
FurtherDerived fd;
fd.foo();
}
输出是 self is 14FurtherDerived
,self 推导成了 FurtherDerived 而不是期望的模板参数 Derived。
解决方法是直接不用 deducing this。(也可以给被注入的类加 final,但是不合理,因为增加了约定)