小编Jan*_*tke的帖子

“返回”C#的返回类型是什么

我正在制作一个控制台应用程序,我有一个“菜单”,用户可以在其中输入信息来创建一个新的 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 有返回类型吗?这样做的正确方法是什么?

c# console return return-type

9
推荐指数
2
解决办法
1126
查看次数

为什么 GCC 不完全支持 C++20 模块?

查看GCC C++ 语言功能支持状态页面,我发现 C++20 模块支持的两个方面仍然缺失,其中之一是部分支持:

  1. P1766R1:减轻小模块问题
  2. P1815R2:翻译单元本地实体
  3. P1103R3:合并模块

考虑到我们已经 2022 年了,为什么会出现这种情况?对于实施这些措施是否存在分歧?这仅仅是开发人员时间不足的问题吗?

c++ gcc g++ c++20 c++-modules

9
推荐指数
0
解决办法
2506
查看次数

什么是 mdspan,它的用途是什么?

在过去一年左右的时间里,我注意到 StackOverflow 上有一些与 C++ 相关的答案mdspan,但我从未在 C++ 代码中真正看到过这些答案。我尝试在 C++ 编译器的标准库目录和C++ 编码指南中查找它们- 但找不到它们。我确实找到了std::span;我猜它们是相关的——但是如何呢?添加“md”代表什么?

请解释一下这个神秘实体的用途,以及我何时需要使用它。

c++ c++-faq std-span mdspan

9
推荐指数
1
解决办法
2480
查看次数

在 C23 之前如何对空指针使用“nullptr”?

在C23中,nullptr关键字得到了标准化。我也更愿意使用C23 之前nullptr的版本NULL,因为这意味着我可以编写可编译的代码:

  • C,C23之前
  • C,从 C23 开始
  • C++

我可以简单地NULL在两种语言和每个 C 标准中使用,但这在 C++ 代码中是非常不寻常的,并且nullptr无论如何正在成为两种语言的规范。

#define据我所知,语言中不允许使用替换关键字,这可能会在定义兼容性宏时引起问题。基本上,我需要:

// only if this is neither C++ nor C23
#define nullptr /* something */
Run Code Online (Sandbox Code Playgroud)

我怎样才能正确定义这个宏?

c c-preprocessor c23

9
推荐指数
2
解决办法
677
查看次数

在 32 位 GCC 中,一个大数字如何精确地放入“double”中?

考虑以下代码:

#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)

c++ floating-point gcc compiler-bug

9
推荐指数
2
解决办法
705
查看次数

如何使用 __builtin_ctz 加速二进制 GCD 算法?

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

8
推荐指数
1
解决办法
328
查看次数

鉴于 C++23 对 constexpr 的放宽,constexpr 不能成为默认值吗?

关键字constexpr在引入 C++11 标准时对其函数实施了相当严格的限制。C++14 和 C++20 放宽了这些限制(最值得注意):

  • C++14 允许多个return语句static_assert等。
  • C++20 允许try并且asm

C++23 进一步软化了这些限制。从我在cppreference中看到的,constexprfor函数似乎只剩下以下含义:

  • 它不能是协程
  • 对于构造函数和析构函数,该类必须没有虚拟基类
  • 对于 constexpr 函数模板和类模板的 constexpr 成员函数,至少一种特化必须满足上述要求。

C++23 甚至删除了 constexpr 函数必须在编译时对于p2448r2中的任何类型“可计算”的限制。根据我的理解,这完全消除了constexpr在编译时评估函数的想法。

是这样吗?如果是这样,constexpr函数还有什么用处呢?

c++ language-design constexpr constexpr-function c++23

8
推荐指数
1
解决办法
1100
查看次数

为什么编译器在内联时不虚拟化对最终类的调用?

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()?这是错过的优化,还是调用有可能是虚拟的?

请参阅Compiler Explorer 上的实时示例

c++ clang compiler-optimization devirtualization

8
推荐指数
1
解决办法
302
查看次数

可以在 constexpr 函数中声明静态局部变量吗?

可以在函数中使用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,或者
  • 只是 …

c++ constants constexpr c++23

8
推荐指数
1
解决办法
205
查看次数

如何设计一个需要在其析构函数中加入一个线程的基类,该线程对同一个类实例进行操作?

我刚刚得到了一个有趣的比赛条件。考虑以下类:

#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)

c++ polymorphism multithreading race-condition stdthread

8
推荐指数
0
解决办法
109
查看次数