我正在制作一个控制台应用程序,我有一个“菜单”,用户可以在其中输入信息来创建一个新的 Person 对象。下面是一个方法里面。
Write("Please enter the first name: ", false);
string fName = Console.ReadLine().ToUpper();
Write("Please enter the middle initial: ", false);
string mInitial = Console.ReadLine().ToUpper();
Write("Please enter the last name: ", false);
string lName = Console.ReadLine().ToUpper();
Run Code Online (Sandbox Code Playgroud)
像这样。我希望用户能够在他们决定不想成为新人时随时退出该方法。所以我想创建一个名为“CheckExit”的新方法,如果他们输入“EXIT”,它将离开“CreatePerson”方法。所以我希望“CheckExit”返回一个回报。否则我必须在每次输入后添加一个“if”语句,这会变得混乱。
这可能吗?return 有返回类型吗?这样做的正确方法是什么?
查看GCC C++ 语言功能支持状态页面,我发现 C++20 模块支持的两个方面仍然缺失,其中之一是部分支持:
考虑到我们已经 2022 年了,为什么会出现这种情况?对于实施这些措施是否存在分歧?这仅仅是开发人员时间不足的问题吗?
在过去一年左右的时间里,我注意到 StackOverflow 上有一些与 C++ 相关的答案mdspan,但我从未在 C++ 代码中真正看到过这些答案。我尝试在 C++ 编译器的标准库目录和C++ 编码指南中查找它们- 但找不到它们。我确实找到了std::span;我猜它们是相关的——但是如何呢?添加“md”代表什么?
请解释一下这个神秘实体的用途,以及我何时需要使用它。
在C23中,nullptr关键字得到了标准化。我也更愿意使用C23 之前nullptr的版本NULL,因为这意味着我可以编写可编译的代码:
我可以简单地NULL在两种语言和每个 C 标准中使用,但这在 C++ 代码中是非常不寻常的,并且nullptr无论如何正在成为两种语言的规范。
#define据我所知,语言中不允许使用替换关键字,这可能会在定义兼容性宏时引起问题。基本上,我需要:
// only if this is neither C++ nor C23
#define nullptr /* something */
Run Code Online (Sandbox Code Playgroud)
我怎样才能正确定义这个宏?
考虑以下代码:
#include <iostream>
int main() {
long long x = 123456789123456789;
std::cout << std::fixed;
auto y = static_cast<double>(x); // (1)
std::cout << static_cast<long long>(y) << "\n"; // (2)
std::cout << y << "\n";
std::cout << (x == static_cast<long long>(y)) << "\n"; // (3)
std::cout << static_cast<long long>(static_cast<double>(x)) << "\n"; // (4)
std::cout << (x == static_cast<long long>(static_cast<double>(x))) << "\n"; // (5)
}
Run Code Online (Sandbox Code Playgroud)
当在 Linux 上用 32 位 GCC 编译时 ( g++ -m32 a.cpp),打印如下:
123456789123456784
123456789123456784.000000
0
123456789123456789 …Run Code Online (Sandbox Code Playgroud) clang 和 GCC 有一个int __builtin_ctz(unsigned)功能。这会计算整数中的尾随零。关于这一系列函数的维基百科文章提到可以使用 加速二进制 GCD 算法__builtin_ctz,但我不明白如何。
二进制 GCD的示例实现如下所示:
unsigned int gcd(unsigned int u, unsigned int v)
{
// simple cases (termination)
if (u == v)
return u;
if (u == 0)
return v;
if (v == 0)
return u;
// look for factors of 2
if (~u & 1) // u is even
if (v & 1) // v is odd
return gcd(u >> 1, v);
else // …Run Code Online (Sandbox Code Playgroud) c++ algorithm bit-manipulation built-in greatest-common-divisor
关键字constexpr在引入 C++11 标准时对其函数实施了相当严格的限制。C++14 和 C++20 放宽了这些限制(最值得注意):
return语句static_assert等。try并且asmC++23 进一步软化了这些限制。从我在cppreference中看到的,constexprfor函数似乎只剩下以下含义:
C++23 甚至删除了 constexpr 函数必须在编译时对于p2448r2中的任何类型“可计算”的限制。根据我的理解,这完全消除了constexpr在编译时评估函数的想法。
是这样吗?如果是这样,constexpr函数还有什么用处呢?
struct base {
virtual void vcall() = 0;
};
struct foo final : base {
void vcall() final;
};
void call_base(base& b) {
b.vcall();
}
void call_foo(foo& f) {
call_base(f);
}
void call_foo_directly(foo& f) {
f.vcall();
}
Run Code Online (Sandbox Code Playgroud)
clang 16 产生:
call_base(base&):
mov rax, qword ptr [rdi]
jmp qword ptr [rax]
call_foo(foo&):
mov rax, qword ptr [rdi]
jmp qword ptr [rax]
call_foo_directly(foo&):
jmp foo::vcall()@PLT
Run Code Online (Sandbox Code Playgroud)
GCC 和 MSVC 产生相同的结果,因此这不是仅限于 clang 的问题。是否也应该call_foo包含非虚拟调用foo::vcall()?这是错过的优化,还是调用有可能是虚拟的?
可以在函数中使用static局部变量吗?constexpr例如:
#include <string_view>
#include <utility>
enum class axis {
x, y, z
};
constexpr std::string_view axis_name(axis a) {
// use static constexpr to avoid putting the table onto the stack
static constexpr std::string_view names[] {
"x", "y", "z"
};
return names[std::to_underlying(a)];
}
constexpr auto x_name = axis_name(axis::x);
Run Code Online (Sandbox Code Playgroud)
GCC 12 无法编译此错误:
<source>:9:39: error: 'names' defined 'static' in 'constexpr' context
9 | static constexpr std::string_view names[] {
| ^~~~~
Run Code Online (Sandbox Code Playgroud)
其他编译器允许它。规则是什么?什么时候允许?
static一般使用,或者static const,或者我刚刚得到了一个有趣的比赛条件。考虑以下类:
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
class A
{
std::thread th;
std::atomic_bool stop = false;
public:
A() = default;
A(const A &) = delete;
A &operator=(const A &) = delete;
~A()
{
stop.store(true);
th.join();
}
virtual void Step() = 0;
void Start()
{
th = std::thread([this]
{
while (true)
{
if (stop.load())
return;
// Just to help reproduce the race condition.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
Step();
}
});
}
};
struct B : A
{
void Step() override
{
std::cout …Run Code Online (Sandbox Code Playgroud)