从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态
周刊项目地址|在线地址 |知乎专栏 | 腾讯云+社区 |
欢迎投稿,推荐或自荐文章/软件/资源等,请提交 issue
标准委员会动态/ide/编译器信息放在这里
编译器信息最新动态推荐关注hellogcc公众号 本周更新2022-04-20 第146期
#include <tuple>
#include <cassert>
int main() {
auto [first, ...ts] = std::tuple{1, 2 ,3};
assert(1 == first);
}
看个乐。提案中
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> createStrings() {
return {"This", "is", "a", "vector", "of", "strings"};
}
int main()
{
for (auto w: createStrings()) {
std::cout << w << " "; // this works fine
}
std::cout << std::endl;
for (auto c: createStrings()[0]) {
std::cout << c << " "; // this is UB
}
std::cout << std::endl;
}
经典bug :range for里面的变量生命周期有问题
聪明的你想到了用optional,照样不行
#include <iostream>
#include <optional>
#include <string>
#include <vector>
std::vector<std::string> createStrings() {
return {"This", "is", "a", "vector", "of", "strings"};
}
std::optional<std::vector<int>> createOptionalInts() {
return std::vector<int>{1,2,3,4,5,6};
}
int main()
{
for (auto i: createOptionalInts().value()) {
std::cout << i << " "; // UB
}
const auto v = createOptionalInts().value(); //注意,必须是值,写成const auto& 一样是UB
for (auto i: v) {
std::cout << i << " ";
}
std::cout << std::endl;
}
//0 0 27344912 0 5 6 1 2 3 4 5 6
其实range for是语法糖,上面的代码等价于
#include <iostream>
#include <optional>
#include <string>
#include <vector>
std::optional<std::vector<int>> createOptionalInts() {
return std::optional<std::vector<int>>1;
}
int main()
{
auto&& range = createOptionalInts().value();
auto position = range.begin();
auto end = range.end();
for(; position != end; ++position) {
std::cout << *(position) << " "; // UB
}
std::cout << std::endl;
}
问题就在这个range已经消失了,访问这个指针就有问题
有点看不懂了
auto timedOut = std::make_shared<bool>();
auto widgetOperation = GetWidgetAsync();
auto widgetTimeout = [](auto timedOut) -> IAsyncOperation<Widget>
{
co_await winrt::resume_after(15s);
*timedOut = true;
co_return nullptr;
}(timedOut);
auto widget = co_await winrt::when_any(widgetOperation, widgetTimeout);
widgetOperation.Cancel();
widgetTimeout.Cancel();
if (*timedOut) {
// timed out
} else {
// GetWidgetAsync() produced something (possibly nullptr)
}
auto widgetOperation = GetWidgetAsync();
auto widgetTimeout = [] -> IAsyncOperation<Widget>
{
co_await winrt::resume_after(15s);
co_return nullptr;
}();
auto widget = co_await winrt::when_any(widgetOperation, widgetTimeout);
auto timedOut = widgetTimeout.Status() == AsyncStatus::Completed;
widgetOperation.Cancel();
widgetTimeout.Cancel();
if (timedOut) {
// timed out
} else {
// GetWidgetAsync() produced something (possibly nullptr)
}
CTAD把活交给了编译器推导,但大家没咋用,还是有make_xx函数来构造对象,清晰,明确
nvc++ compiler
软文的感觉。介绍了一些算法可以并行,比如
// Step 1: compute the number of variables contributed by every node.
int* numValuesPtr = allocateMemory(numberOfCells);
for_each(execution::par_unseq, numValuesPtr,
numValuesPtrl + numberOfCells, [=](int& numValues)
{
int i = &numValues - numValuesPtr;
// Compute number of variables contributed by current node.
numValues = computeNumValues(i);
} );
// 2. Compute the buffer index for every node.
int* indexPtr = allocateMemory(numberOfCells);
exclusive_scan(execution::par_unseq, numValuesPtr,
numValuesPtr + numberOfCells, indexPtr, 0);
// 3. Pack the data into the buffer.
for_each(execution::par_unseq, indexPtr,
indexPtr + numberOfCells, [=](int& index)
{
int i = &index - indexPtr;
packCellData(i, index);
} );
介绍gdb启动都做了什么以及如何优化启动速度
介绍vscode更新的调试功能(谁用vscode调试啊)
namespace tool {
inline namespace v1_0_0 {
struct Data {
int a;
bool b;
std::string c;
};
bool foo(const Data d);
}
}
int main() {
const tool::Data d;
return tool::foo(d);
}
看到这个用法,inline namespace在调用的时候可以省掉,但是这个inline namespace的符号可以保证唯一,这样就避免了不同版本造成的ABI break
很妙,但没人用。这属于项目管理的一部分。严格来说很难出现ABI break
2024-0121更新:
和群友讨论,如果用using namespace detail_v1_0_0那种嵌套命名空间的玩法,其实也可以
inline namespace能有啥优势,省符号?
我写了两版对比 https://godbolt.org/z/TK1PTf4bG 实际上汇编符号基本差不多,没省什么玩意。
inline省一行,也行吧
互联网寒冬了胖友们