C++ 虐我千百遍
109 subscribers
31 photos
7 links
Download Telegram
Forwarded from Lancern🧧
#define FN(f) \
[&]<typename ...Args>(Args &&...args) -> decltype(auto) \
requires requires { (f)(std::forward<Args>(args)...); } \
{ return (f)(std::forward<Args>(args)...); }
TIL:lambda 的 noexcept 必须放在 trailing return type 的前面,与参数有关的 requires 必须放在 trailing return type 的后面
所以正确顺序是
[&]
<typename... Ts>
requires something<Ts...>
[[noreturn]]
(Ts&&... ts)
mutable
noexcept(true)
[[another_attr]]
-> decltype(auto)
requires requires { someThing(ts...); }
pre(somePre(ts...))
{
ohFinallyTheBody();
}
TIL: 0f 是不合法的,要写 0.f
今日被坑:x86_64 上,多线程并发写入 vector 的不同位置可能出现 race
什么茴字有四种写法都弱爆了,你知道 flat_map 有 28 种构造方法吗?
今日踩坑:uint16_t bit_or uint16_t -> int
TIL:string stringstream::str() && 这个 zero-copy 功能到 C++20 才有
Forwarded from valueless channel
#今天踩了啥坑 #cpp #cpp20
template<size_t i>
consteval void count_to_one() {
if constexpr (i <= 1) {
return;
}

char buffer[i];
if constexpr (i % 2 == 0) {
// ... do something if `i` is even
return count_to_one<i - 1>();
} else {
// ... do something if `i` is odd
return count_to_one<i - 1>();
}
}

int main() {
count_to_one<10>();
}

先试试你能一眼看出代码中的错误吗

尽管走到判断 i 的奇偶处,一定隐含了 i 不会小于等于 1,但在 if constexpr 的上下文中,有无 else 关键字对编译器来说是有区别的 —— if constexpr 上下文以外的代码对于编译器总是可见的(不受编译时常量条件编译),所以上面的代码会导致实例化出 i 下溢出到负数长度的 buffer 数组。

https://godbolt.org/z/nPz9WzxrK
TIL
如果 x 是以 const auto&& 定义的,那么 return x 会尝试进行拷贝构造;如果 x 是以 auto&& 定义的,则 return x 会走 forced ellision
怎么 vector<T>&& 的 operator[] 得到的不是 T&&
男生说话像“C++ 编译器”的方式:
Forwarded from Soha Jin
之后会发生什么就不保证了.jpg
听君一席话
Forwarded from 橘橘橘子汁 & 🍊
颜文字这块
TIL std::fma
而且居然是 C++11 就有了!!在 cmath 里面!!
Forwarded from Lancern🧧
噢对了,可以给群友介绍一下,因为 cppreference 已经 readonly 快一年了,也不知道什么时候他们能够把系统迁移好,我们正在打算重建一个 C/C++ 的文档网站并把 cppreference 的内容迁移过来,感兴趣的群友可以看一下 https://cppdoc.cc/ ,仓库在 https://github.com/cppdoc-cc/cppdoc