标签: c++20

为什么最新的 clang 没有定义功能测试宏 __cpp_coroutines?

#include <iostream>

int main() {
#if __has_include(<coroutine>)
    std::cout << "__has_include(<coroutine>)" << std::endl;
#endif

#if defined(__cpp_impl_coroutine)
    std::cout << "__cpp_impl_coroutine is defined." << std::endl;
#endif

#if defined(__cpp_coroutines)
    std::cout << "__cpp_coroutines is defined." << std::endl;
#else
    std::cout << "__cpp_coroutines IS NOT defined!" << std::endl;
#endif
}
Run Code Online (Sandbox Code Playgroud)

我的编译器是 clang-18.1.0。

使用 构建代码clang++ -std=c++20 -stdlib=libc++ ./main.cpp,输出为:

__has_include(<coroutine>)
__cpp_impl_coroutine is defined.
__cpp_coroutines IS NOT defined!
Run Code Online (Sandbox Code Playgroud)

为什么最新的 clang 没有定义功能测试宏 __cpp_coroutines?

c++ clang compiler-bug c++20 c++-coroutine

0
推荐指数
1
解决办法
85
查看次数

标准空参数类型的功能类似于 void

标准中是否有类似的类型void?即实际上从参数列表中省略的东西,如标签式结构?

void foo(){}
void foo(std::empty_type){}
// assembly for these two should be the same, no arguments
Run Code Online (Sandbox Code Playgroud)

我的用例:我正在编写自己的基于范围的for迭代器对象。问题是返回的对象足以检查它是否在最后(我正在包装一个 API)。这种基于范围的for受到了很大的打击,任何小的优化都会对代码性能产生巨大的提升。问题是,我不能声明一元,operator !=也不能void按照标准声明类型的符号,所以我想用大小为 0 的对象来模拟它。

我可以使用什么类型来提示编译器完全省略参数?会像std::monostate工作吗?从我对 MSVC 的测试来看,using empty_type = struct {}不是解决方案。

c++ c++20

-1
推荐指数
1
解决办法
81
查看次数

为什么 C++20 中的空结构没有隐式飞船运算符?

动机:有时我使用 std::variant 来实现“花式”枚举,其中一些枚举状态可以携带状态。

现在,如果我想将<=>用于我的变体,它需要我的空结构已经定义 <=>。这对我来说似乎有点奇怪,因为如果类型具有 0 位状态,则该类型的所有实例都是相同的。

完整示例

#include <compare>
#include <iostream>
#include <variant>

struct Off{
    // without this the code does not compile
    auto operator<=>(const Off& other) const = default;
};

struct Running{
    int rpm=1000;
    auto operator<=>(const Running& other) const = default;
};

using EngineState = std::variant<Off, Running>;

int main()
{
    EngineState es1, es2;
    es1<=>es2;
}
Run Code Online (Sandbox Code Playgroud)

c++ spaceship-operator c++20

-1
推荐指数
1
解决办法
119
查看次数

为什么 g++-11 '-O2' 包含错误而 '-O0' 没问题?

#include <limits>
#include <cstdint>
#include <iostream>

template<typename T>
T f(T const a = std::numeric_limits<T>::min(),
    T const b = std::numeric_limits<T>::max())
{
    if (a >= b)
    {
        throw 1;
    }

    auto n = static_cast<std::uint64_t>(b - a + 1);
    if (0 == n)
    {
        n = 1;
    }

    return n;
}

int main()
{
    std::cout << f<int>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

g++-11 -std=c++20 -O2应该输出0比其他1

clang++ 没问题。如果我-O2改为-O0,g++-11 也可以。

参见:在线演示

为什么 g++ -O2 包含一个错误而 -O0 可以?

c++ gcc g++ internal-compiler-error c++20

-1
推荐指数
1
解决办法
88
查看次数

执行 PIMPL 时如何避免共享指针开销

AFAIKunique_ptr与 PIMPL 一起使用非常棘手,因为删除器是unique_ptr类型的一部分,因此它不适用于不完整的类型。另一方面,shared_ptr使用动态删除器,因此它可以处理不完整的类型。

shared_ptr无论我是否需要,都存在给我原子操作的性能问题。

我可以使用其他更快的替代方案吗std::?我显然对类型擦除很满意,我说的是原子引用计数的成本。

#include <any>
#include <memory>
#include <iosfwd>

std::shared_ptr<std::fstream> sp;
// unique_ptr requires complete type
// std::unique_ptr<std::fstream> up;
std::any a;

#include <fstream>
int main() {
    // any requires copy_constructible
    // a = std::fstream{};  
    sp = std::make_shared<std::fstream>();
} 
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 我考虑过any,但它不适用于某些类型。
  • 我考虑使用动态删除器的 unique_ptr ,但据我所知构造unique_ptr函数永远不会“告诉”删除器构造的对象是什么(为删除器提供一种学习如何销毁对象的方法)。

PS 我知道很久以前boost::shared_ptr就有宏来禁用原子引用计数,但即使仍然支持我也不想切换到boost::shared_ptr.

c++ pimpl-idiom shared-ptr c++20

-1
推荐指数
1
解决办法
285
查看次数

单个字符的常量全局数组?

我尝试编写,const char TabArray[32]{'\t'};因为我想将最多 32 个字节的选项卡传递到函数中(不需要 null),并且编写\t32 次= "\t\t会使我的源代码看起来很丑

如果我想要它 const 我还有其他选择吗?

c++ c++20

-1
推荐指数
1
解决办法
91
查看次数

在检查两个字符串是否不区分大小写相等时,变换视图而不是投影仪会降低 C 风格方法的性能

太长了;博士

\n

在检查两个字符串是否不区分大小写相等时,为什么这段代码

\n
bool rangeV3WithTransformViews(std::string_view str1, std::string_view str2) {\n    return equal(str1 | transform(tolowercase),\n                 str2 | transform(tolowercase));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

比这个慢很多吗?

\n
bool rangeV3WithProj(std::string_view str1, std::string_view str2) {\n    return equal(str1, str2, std::equal_to{}, tolowercase, tolowercase);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

从后面的代码中,GCC 13.2-O3设法生成与低级 C 风格解决方案相同的汇编代码:

\n
#define to_lower_ascii(c) (((c) >= \'A\' && (c) <= \'Z\') ? (c) + 32 : (c))\n\nint cstyle(const char* str1, const char* str2) {\n    while (*str1 != \'\\0\' && *str2 != \'\\0\' && to_lower_ascii(*str1) == to_lower_ascii(*str2)) {\n        str1++;\n        str2++;\n    }\n    return to_lower_ascii(*str1) …
Run Code Online (Sandbox Code Playgroud)

c++ performance benchmarking range-v3 c++20

-3
推荐指数
1
解决办法
151
查看次数

现代C ++是否会支持C#“对象初始化器”语法?

我很好奇,如果现代的c ++标准人们已经考虑或添加了C#的“对象初始化器”语法。例如,在C#中,我可以在初始化期间像这样初始化一个对象的成员:

StudentName student2 = new StudentName
{
    FirstName = "Craig",
    LastName  = "Playstead"
};

public class StudentName {
    public string FirstName;
    public string LastName;
};
Run Code Online (Sandbox Code Playgroud)

如果人们计划将标准的“对象初始值设定项”语法添加到现代C ++中,它将非常方便。C ++ 11,C ++ 14,C ++ 17,C ++ 20等

它当前是否存在于现代C ++规范中?

c++ c++20

-4
推荐指数
1
解决办法
265
查看次数