RSS https://github.com/wanghenshui/cppweeklynews/releases.atom
欢迎投稿,推荐或自荐文章/软件/资源等
请后台留言
另外公众号挂了c++templates 第二版优惠
从上面的链接里下单的兄弟买书到货后可以找我退佣金,加我微信,公众号后台回复即可
本期文章由 黄亮anthony 不语 赞助
标准委员会动态/ide/编译器信息放在这里
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-12-20 第233期
委员会邮件 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/#mailing2023-12
本月委员会邮件没有什么新鲜的,顶多fiber_context。这里不展开了
template<auto N> consteval auto nth(auto... ts) { return ts...[N]; }
static_assert(1 == nth<0>(1, 2, 3));
static_assert(2 == nth<1>(1, 2, 3));
static_assert(3 == nth<2>(1, 2, 3));
还不确定有什么作用
const的副作用
经典例子
#include <iostream>
int main() {
const int i = 9;
int& j = const_cast<int&>(i);
j = 4;
std::cout << i << std::endl; // prints 9
std::cout << j << std::endl; // prints 4
}
https://godbolt.org/z/vGG3cdavE
但是这个例子,返回优化了,即使没有实现move
#include <iostream>
#include <cassert>
class Rng {
int _min;
int _max;
// invariant: _min <= _max
public:
Rng(int lo, int hi)
// precond: lo <= hi
: _min(lo), _max(hi)
{ assert(_min <= _max); }
int const& min() const { return _min; }
int const& max() const { return _max; }
void set(int lo, int hi)
// precond: lo <= hi
{
_min = lo;
_max = hi;
assert(_min <= _max);
}
Rng(Rng&&) { assert(false); } // this is never called
Rng(Rng const&) { assert(false); } // this is never called
};
const Rng foo() {
const Rng r {1, 2};
std::cout << &r << std::endl;
return r;
}
Rng bar() {
Rng r = foo();
r.set(3, 4);
std::cout << &r << std::endl;
return r;
}
int main() {
const Rng z = bar();
std::cout << &z << std::endl;
}
https://godbolt.org/z/n9nn5GjMM
注意这两个例子的区别,统一作用域上的修改
上面的这个xyz 本质上就是一个对象,和第一个例子同一个域里const_cast导致变化不同
怎么mock时间?比如特化?
template <typename ...>
constexpr auto clock_impl = std::chrono::some_clock{};
template <typename ... Ts>
struct app_clock {
static
std::chrono::some_clock::time_point now()
{
return clock_impl<Ts...>.now();
}
};
struct test_clock {
using time_point = std::chrono::some_clock::time_point;
static time_point now() { return {};}
};
template <>
constexpr auto clock_impl<> = test_clock{};
https://godbolt.org/z/GbWYaGc7q
讲的不错
写的很有深度,值得一看
省流 arm O3 优化bug
-ftrivial-auto-var-init=[pattern|zero|uninitialized]
帮助自动初始化栈上的局部变量
开销很大,研究了一圈放弃了
一种是make_unique这种需要指定T的,一种是swap sort这种不指定T的
如何跨过这种边界,有设计,比如CTAD,但这并不建议使用
那就只能多提供重载了,比如optional
template<class T, class A>
optional<T> make_optional(A);
template<class A>
optional<A> make_optional(A);
然后她举了个例子,怎么设计强制制定T和忽略T
https://godbolt.org/z/h38PhG3Y6
#include <type_traits>
#include <iostream>
//template<class T, class A>
//T implicitly_convert_to(std::type_identity_t<A>) = delete;
template<class T, class A,
std::enable_if_t<std::is_convertible_v<A, T>, int> E = 0>
T implicitly_convert_to(A arg) { return T(arg); }
int main() {
//auto i0 = implicitly_convert_to(9.9999999);
//std::cout << i0 << "\n";
auto i1 = implicitly_convert_to<int>(9.9999999);
std::cout << i1 << "\n";
//auto j2 = implicitly_convert_to<int, float>(9.9999999);
//std::cout << j2 <<"\n";
return 0;
}
看一乐
直接贴代码了
https://godbolt.org/z/MvYfbEP8r
https://godbolt.org/z/57zsK9rEn
设计的挺有意思的,鉴于篇幅,放在后面
constexpr的代码 编译器没有做充分的优化。这可能加剧编译时长
算是个坑爹细节。运行时能充分优化的代码到了constexpr期反而没优化了
寻路算法,A*之类的,如何缓存友好。STL不太行
valgrind 也可以测试cache性能,判断miss
valgrind --tool=cachegrind --cache-sim=yes
perf也可以,就不说了
结论就是 顺序访问 不要跳转 只访问用到的数据 s执行路径里没有malloc
比如std::unordered_multimap::equal_range 内存不连续,miss就很多
概念很帅,把range推广到分布式,做的一些工作
代码在这里 https://github.com/oneapi-src/distributed-ranges/tree/main
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有