#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?
标准中是否有类似的类型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 {}
不是解决方案。
动机:有时我使用 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) #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
可以?
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
函数永远不会“告诉”删除器构造的对象是什么(为删除器提供一种学习如何销毁对象的方法)。PS 我知道很久以前boost::shared_ptr
就有宏来禁用原子引用计数,但即使仍然支持我也不想切换到boost::shared_ptr
.
我尝试编写,const char TabArray[32]{'\t'};
因为我想将最多 32 个字节的选项卡传递到函数中(不需要 null),并且编写\t
32 次= "\t\t
会使我的源代码看起来很丑
如果我想要它 const 我还有其他选择吗?
在检查两个字符串是否不区分大小写相等时,为什么这段代码
\nbool 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比这个慢很多吗?
\nbool 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 风格解决方案相同的汇编代码:
#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 ++标准人们已经考虑或添加了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++ ×8
c++20 ×8
benchmarking ×1
clang ×1
compiler-bug ×1
g++ ×1
gcc ×1
performance ×1
pimpl-idiom ×1
range-v3 ×1
shared-ptr ×1