小编Nic*_*els的帖子

循环遍历 chrono 中的所有月份

我需要在代码中迭代所有月份:

for (auto m = std::chrono::January; m <= std::chrono::December; m++)
  std::cout << std::format(std::locale { "pt_BR.UTF-8" }, "{:L%B}\n", m);
Run Code Online (Sandbox Code Playgroud)

但它恰好是一个无限循环,因为std::chrono::month增量具有模 12 + 1 行为。因此,当循环m变量达到December(12) 时,它会回绕到January(1)。

我尝试过std::views::iota

for (auto m : std::views::iota(std::chrono::January) | std::views::take(12))
  std::cout << std::format(std::locale { "pt_BR.UTF-8" }, "{:L%B}\n", m);
Run Code Online (Sandbox Code Playgroud)

std::chrono::month但由于不满足概念而无法编译std::weakly_incrementable

c++ c++11 c++-chrono

6
推荐指数
1
解决办法
273
查看次数

std::osyncstream 的用途?

std::osyncstream新的 C++20 ( http://en.cppreference.com/w/cpp/io/basic_osyncstream )有什么用途?不是std::ostream已经线程安全了吗?

c++ c++20

5
推荐指数
1
解决办法
2474
查看次数

C++20 格式的异常消息

类似于这个问题。需要使用类似 printf 的风格而不是字符串串联或 iostreams 来抛出异常和消息。使用 C++ 20 格式化库:

  throw std::runtime_error { 
    std::format("Critical error! Code {}: {}", errno, strerror(errno)) 
  }; 
Run Code Online (Sandbox Code Playgroud)

但在所有带有格式化的异常中,感觉调用格式都不符合人体工学,可以变得更好吗?

c++ c++20 stdformat

5
推荐指数
1
解决办法
347
查看次数

如何使用 span 来包装命令行参数

即将推出的 C++20 的这种使用是否std::span正确并且没有开销来包装命令行参数?

#include <iostream>
#include <span>

int main(int argc, const char* argv[])
{
    for (auto s : std::span { argv, static_cast<std::size_t>(argc) })
      std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如果它是正确的,我可以进一步使用 withstd::string_view吗?

c++ string-view c++20 std-span

2
推荐指数
1
解决办法
1532
查看次数

标签 统计

c++ ×4

c++20 ×3

c++-chrono ×1

c++11 ×1

std-span ×1

stdformat ×1

string-view ×1