我想做以下事情:
// function that depends on key to do stuff
template <int key>
void bar() {...}
template <int ...Keys>
void foo(int key) {
// WHAT SHOULD BE HERE?
}
std::cin >> key;
foo<1,3,5,7,9>(key);
Run Code Online (Sandbox Code Playgroud)
使得它变成
template <int ...Key>
void foo(int key) {
switch (key) {
case 1: bar<1>();break;
case 3: bar<3>();break;
case 5: bar<5>();break;
case 7: bar<7>();break;
case 9: bar<9>();break;
default: break;
}
}
Run Code Online (Sandbox Code Playgroud)
如何生成一个 switch 语句,将所有可变参数模板参数枚举为高效的 switch 语句,而无需手动编写 switch 语句?
我有这个:
#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
int val = 500;
}
namespace
{
int val = 400;
}
// Global variable
//int val = 100;
int main()
{
// Local variable
int val = 200;
// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << '\n';
cout << ::val << '\n';
cout << val << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况::val …
我需要在代码中迭代所有月份:
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。
我故意在行的开头提供一些注释,例如一些调试代码,并且不想缩进这些行。我同意 clang-format 根本不碰//评论。他们全部。我发现的唯一设置ReflowComments=false仍然可以识别评论。
我没有找到任何设置来不缩进注释,如果我执行以下操作
{
// clang-format off
//comment
// clang-format on
}
Run Code Online (Sandbox Code Playgroud)
已重新格式化
{
// clang-format off
//comment
// clang-format on
}
Run Code Online (Sandbox Code Playgroud)
这真是太可怕了。当然我希望那也clang-format不会碰触//clang-format off和//clang-format on线条。
我正在阅读 C++ Concurrency In Action,第 32 页(第 2 章)中有这段代码。
template <typename Iterator, typename T>
struct accumulate_block
{
void operator()(Iterator first, Iterator last, T &result)
{
result = std::accumulate(first, last, result);
}
};
template <typename Iterator, typename T>
T parallel_accumulate(Iterator first, Iterator last, T init)
{
unsigned long const length = std::distance(first, last);
if (!length)
return init;
unsigned long const min_per_thread = 25;
unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread;
unsigned long const hardware_threads = …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的程序来打印指向 char 的指针的地址,但是当我运行此代码时,它会导致内存泄漏。
我正在使用termux on android设备。我用来运行该文件的命令是
g++ -Wall -Wextra -fsanitize=address -o out filename.cpp && ./out
#include <iostream>
using namespace std;
int main(void) {
char ch = 'a';
char *ptr = &ch;
cout << ptr << endl;
return 0;
Run Code Online (Sandbox Code Playgroud)
}
输出
=================================================================
==9156==ERROR: AddressSanitizer: stack-buffer-overflow on address 0xfffd3c71 at pc 0xf4de6974 bp 0xfffd3c30 sp 0xfffd3808
READ of size 9 at 0xfffd3c71 thread T0
#0 0xf4de6970 in strlen out/lib/compiler-rt-arm/out/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:372:5
#1 0x4cab5c in std::__ndk1::char_traits<char>::length(char const*) (/data/data/com.termux/files/home/dircpp/out+0x2b5c)
#2 0x4ca144 in std::__ndk1::basic_ostream<char, std::__ndk1::char_traits<char>>& …Run Code Online (Sandbox Code Playgroud) 我想用 和 的标记分割字符串std::views::split,并为每个检索到的子字符串调用该std::from_chars函数。这是一个 MRE ( https://godbolt.org/z/1K71qo9s4 ),它在 GCC 上编译成功,但在 MSVC 上编译失败:
#include <iostream>
#include <ranges>
#include <string>
#include <charconv>
int main()
{
std::string source{"10%15%20%35"};
for ( auto i : source | std::views::split('%') )
{
int x;
std::from_chars( i.begin().base(), i.end().base(), x );
std::cout << x << ' ';
}
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,根据cppreference , P2210R2中的行为std::views::split发生了变化,提供了效果为base()
相当于
return cur_
然后,根据编译器支持页面,P2210R2自 19.31 起 MSVC 支持该示例,因此该示例应该可以工作。
类似于这个问题。需要使用类似 printf 的风格而不是字符串串联或 iostreams 来抛出异常和消息。使用 C++ 20 格式化库:
throw std::runtime_error {
std::format("Critical error! Code {}: {}", errno, strerror(errno))
};
Run Code Online (Sandbox Code Playgroud)
但在所有带有格式化的异常中,感觉调用格式都不符合人体工学,可以变得更好吗?
考虑以下代码:
#include <memory>
#include <optional>
template <typename T>
constexpr T * arg_to_pointer(T & arg) noexcept {
return std::addressof(arg);
}
int main() {
std::optional<int> foo;
auto * test = foo.transform(arg_to_pointer<int>).value_or(nullptr);
//auto * test = foo.transform(std::addressof<int>).value_or(nullptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
未注释掉的行(将一个哑包装器std::addressof作为函数参数传递给std::optional::transform)可以编译并正常工作。被注释掉的行(尝试std::addressof直接使用的行)没有 - 我收到一条错误,指示模板参数推导失败(此处使用 GCC 13.2 的实时示例,尽管在 Clang 16.0 中观察到类似的行为)。为什么是这样?std::addressof标准和我周围的愚蠢包装有什么区别?
不知何故,我仍然认为 lambda 是常规函数对象的“语法糖”,因此令我惊讶的是,在 C++-20 下,有状态但在其他方面的constexprlambda 实例不能用作非类型模板参数,这与等效的函数对象实例不同。
谁能解释这种行为或决定?
Godbolt示例:
struct always_fn {
const int x;
int operator()() const
{
return x;
}
};
inline constexpr always_fn always_f{5};
// lambda equivalent to `always_f`
inline constexpr auto always_2_f = [x = 5]() {
return x;
};
template<typename F>
struct wrapped {
F f_;
};
inline constexpr auto wrapped_f = wrapped{always_f};
inline constexpr auto wrapped_2_f = wrapped{always_2_f};
template<auto f>
void pass() {}
int main() {
pass<always_f>();
pass<wrapped_f>();
// error: no matching …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++20 ×3
c++-chrono ×1
c++11 ×1
c++17 ×1
c++23 ×1
char ×1
clang-format ×1
concurrency ×1
constexpr ×1
format ×1
global ×1
lambda ×1
memory-leaks ×1
non-type-template-parameter ×1
std-ranges ×1
stdformat ×1
stdoptional ×1
stl ×1
templates ×1