
[英]is `return_if_null(T* ptr)` possible?
我发现自己做了很多void doSomethingCool(CoolObject* coolObject) { if (coolObject == nullptr) { return; } // now _actually_ do something cool } ...
[英]is `return_if_null(T* ptr)` possible?
我发现自己做了很多void doSomethingCool(CoolObject* coolObject) { if (coolObject == nullptr) { return; } // now _actually_ do something cool } ...
[英]Why trying to print unicode encoded strings with cout leads to compilation error in newer C++ standards?
我尝试使用 Visual C++ 2022 版本 17.4.4 将 C++ 标准设置为最新来打印 Unicode 个字符。 我有编译错误: 使用u (utf-16) 和U (utf-32) 字符串文字观察到相同的行为。 将标准设置为 C++17 或 C++14 使程序可以编译。 在 C++20 和 ...
[英]CMake set project to C++ 20
我正在尝试将 C++ 标准从版本 17 更改为 20,以便我可以使用 jthread 库。 在我添加的项目的根 CMakelists.txt 上: 但是当我再次构建项目并查看程序运行的版本时,它仍然是 C++ 17 版本而不是 C++ 20。 我检查运行以下行的版本: 我正在使用 g++ (Ubu ...
[英]Is there a way to get the type of a dereferenced value from a dereference-able type?
我在玩 C++ 概念,遇到了一个有趣的问题。 我有以下两个自定义概念: 顾名思义,第一个用于确定给定类型是否可以取消引用,而另一个用于检查类型是否支持 output 运算符。 我还有一个名为println的 function 模板,如下所示: 当且仅当类型T是可取消引用并且取消引用值的类型是可打印 ...
[英]Concept is satisfied by a type that would seemingly produce an invalid expression
在下面的代码中, can_foo概念测试是否可以在类型的实例上调用foo()成员 function。 我将使用它来测试两个模板的实例: base有条件地启用foo成员 function, derived覆盖foo以调用其 base 的实现:template <typename T> co ...
[英]Correct use of std::variant and std::visit when functor requires multiple arguments
我一直在努力让我的代码结合使用std::variant和std::visit来工作。 我现在已经将我的代码减少到最少只在我的变体中使用一种类型,但编译器仍然抱怨没有匹配的访问调用。 我正在使用 gcc 11.3 ...
[英]Can not deduce type of ranges iterator
以下代码无法在 MSVC 2022 和 gcc 12.1(启用 c++20)上编译,并出现相同的错误: (**)行生成一个错误,无法推断出param_type ctor 迭代器的类型: std::discrete_distribution<int>::param_type::param ...
[英]C++20 feature std::bit_cast : what happens to the value while reinterpreting type from to type to
C++20 中的std::bit_cast是reinterpret_cast的更安全版本,它与constexpr一起使用,所以我读到,但这是否意味着所涉及变量的实际值也按位重新解释? 例如下面的简单代码 产生这个 output f: 123.017, f_int: 1123420340 我正在尝 ...
[英]How to build a parameter pack without any input values, from variadric class template?
我有一个 class,它将产生一组值并将它们传递给回调,如可变参数模板 arguments 所定义: 我的真实代码是模板化的 SQL 语句处理程序,因此这些是要从数据库中读取的类型。 为了简化,想象一下这些虚拟方法产生的值: 在实际代码中,这些是转换特征。 有问题的代码试图将生成的值放入回调中。 ...
[英]C++ What is the size of a pointer to an empty class?
长话短说: 指向空 class empty_t的指针的大小是多少? C++20 属性[[no_unique_address]]是否对该指针大小有任何影响? 特别是,第一个似乎是一个相当古老的问题,但很难在 Inte.net 上找到信息(因为搜索被更多关于指针和空基类优化的琐碎信息所破坏)。 更长 ...
[英]Erasing character type and string length in a static string injected as NTTP?
我目前正面临一个与字符串文字和类型擦除有关的模板元编程问题。 问题如下。 让我们考虑以下代码: 问题:如何设计一个 class static_string ,它将作为其类型的一部分“擦除”字符类型及其长度,以便上面的static_assert不会失败? ...
[英]Unresolved external symbols with git submodules
我有一个包含 2 个项目的解决方案: 一库static(杏仁) 使用该 static 库的项目(沙盒) 除了我使用的是 C++20 模块外,我正在观看和关注 The Cherno 的游戏引擎系列。 他通过git submodule add了spdlog 。 我做了同样的事情。 然后我将$(Solut ...
[英]Confusion (or Clang bug?) about incomplete types in std::vector
[vector.overview]/4 中的 C++20 标准状态: 如果分配器满足分配器完整性要求,则在实例化 vector 时可以使用不完整类型 T。 在引用向量的结果特化的任何成员之前,T 应该是完整的。 默认分配器std::allocate确实满足allocator completene ...
[英]Why do the new ranges remove_* algorithms move the first iterator?
当我注意到以下行时,我正在阅读std::ranges::remove的 MSVC STL 实现:_First = _RANGES _Find_if_unchecked(_STD move(_First), _Last, _Pred, _Proj); 实际上, cppreference在其“可能的 ...
[英]Can Niebloids be passed where Callables is required?
一般来说,除非明确允许,否则 C++ 程序尝试获取标准库 function 的指针的行为是未指定的。 这意味着在将它们作为Callable传递之前应格外小心。 相反,通常最好将它们包装在 lambda 中。 有关该主题的更多信息: 我可以获取标准库中定义的 function 的地址吗? 然而,C+ ...
[英]How to get std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> from variables of year,month,day,hour,minute and seconds?
我已经声明了一个 time_point 变量并尝试从年月日等分配值。但是当我使用 sys_days 时它失败了。 代码如下。 代码遇到错误,但我不知道如何修复。 感谢您的帮助。 ...
[英]How to construct a span from std::iota?
以下作品:#include <vector> #include <ranges> int main() { auto view = std::vector<int>{0,1,2,3,4}; auto s = std::span{view.beg ...
[英]Do likelihood attributes make sense with a single if statement?
state Cppreference和本文档并未明确表示似然属性不适用于单个if语句。 或者,我只是不明白alternative path of execution是什么意思。 所以这就是我的问题,例如[[unlikely]]属性是否适用于以下情况? ...
[英]How to avoid using #define macros in C++ in a case where I have to concatenate two const char* variables?
我想在我的代码中删除对 #define 宏的依赖,但我无法使用constexpr实现相同的功能。 实际考虑以下示例: 我已经了解到fmt::format() function 不是一个constexpr function 而它只是一个运行时 function 的艰难方式。我期待我可以在代码上更具表 ...
[英]Platform-extension-specific replacement for volatile-specifier as hardware-register access without compiler optimizations?
在 C++20 及更进一步的版本中,变量的 volatile 说明符在很大程度上已被弃用。 使用 GCC 的嵌入式开发人员长期以来一直依赖这个(有缺陷的)说明符。 代码图中可能出现副作用的概念,代码中的 C++ 语句不清楚,这不是一个好的 C++ 核心语言标准想法。 GCC 实现了它自己的 C+ ...