草庐IT

c++ - std::addressof 作为 C++17 中的常量表达式

std::addressof的规范已针对C++17进行了更改:现在允许它是常量表达式。但是,cppreference说:Theexpressionstd::addressof(E)isaconstantsubexpression,ifEisanlvalueconstantsubexpression.什么是常量子表达式?std::addressof(E)将是常量表达式的示例是什么?std::addressof(E)不是常量表达式的示例是什么? 最佳答案 这是解释here.Introducethefollowingnewdefiniti

c++ - 我可以将 C++17 无捕获 lambda constexpr 转换运算符的结果用作函数指针模板非类型参数吗?

在回答HowdoIwritealambdaexpressionthatlookslikeamethod?时,我试图通过利用以下事实将无捕获lambda转换为成员函数指针,即自C++17以来,无捕获lambda有一个constexpr转换运算符到其函数指针类型。所以我想出了一个归结为:templatestructA{};intmain(){A([]{})>{};//1constexprautofp=static_cast([]{});A{};//2}现在,它在clang中编译(从5.0.0开始)但gcc(>=7.2)提示:error:lambda-expressionintemplate

c++ - 如何在 C++17 中将抛出函数指针静态转换为 noexcept?

C++17使noexcept成为函数类型的一部分。它还允许从noexcept函数指针到潜在的抛出函数指针的隐式转换。void(*ptr_to_noexcept)()noexcept=nullptr;void(*ptr_to_throwing)()=ptr_to_noexcept;//implicitconversionhttp://eel.is/c++draft/expr.static.cast#7表示static_cast可以执行这种转换的逆操作。void(*noexcept_again)()noexcept=static_cast(ptr_to_throwing);不幸的是,GCC

类中的 C++17 变体<any>

以下代码编译良好:intmain(){variantvar;var=5;cout(get(var))但是当我试图把variant作为类(class)成员structMyClass{variantvar;};intmain(){MyClasss;return0;}它无法编译。是我做错了什么还是一些错误?我正在使用gcc7.1.0Infileincludedfrom/home/zak/Projects/Anytest/main.cpp:3:0:/usr/local/gcc-7.1/include/c++/7.1.0/variant:Ininstantiationof‘structstd::

c++ - 在 C++17 中,为什么关联容器有一个 `erase` 成员函数(非 -`const` ) `iterator` ?

见,例如,http://en.cppreference.com/w/cpp/container/map/erase在C++03中有三个重载:voiderase(iteratorpos);voiderase(iteratorfirst,iteratorlast);size_typeerase(constkey_type&key);在C++11中,第一个和第二个重载被更改为采用const_iterator以便可以使用iterator或const_iterator调用它们>。第一个重载也得到了改进,它让迭代器在删除后将迭代器返回到元素:iteratorerase(const_iterator

c++ - 在 C++17 中具有不可移动类型和保证 RVO 的多个返回值(结构化绑定(bind))

在C++17中,我们将有可能返回不可移动(包括不可复制)类型,例如std::mutex,这可以被认为是保证返回值优化(RVO):Guaranteedcopyelisionthroughsimplifiedvaluecategories:structnocopy{nocopy(nocopy&)=delete;nocopy()=default;};autogetRVO(){returnnocopy();}我们也会有structuredbindings,允许:tuplef();auto[x,y,z]=f();或者(这里也使用我对特性templateargumentdeductionforco

c++ - 在 C++17 中排序的移位操作数

我阅读了C++17标准$8.5.7.4:TheexpressionE1issequencedbeforetheexpressionE2.用于移位运算符。还有cppreference第19条规定:InashiftoperatorexpressionE1E2,everyvaluecomputationandside-effectofE1issequencedbeforeeveryvaluecomputationandsideeffectofE2但是当我尝试使用gcc7.3.0或clang6.0.0编译以下代码时#includeusingnamespacestd;intmain(){inti

C++17:显式转换函数 vs 显式构造函数 + 隐式转换——规则改变了吗?

Clang6、clang7和gcc7.1、7.2和7.3都同意以下是有效的C++17代码,但在C++14和C++11下是模棱两可的。MSVC2015和2017也接受它。但是,即使在c++17模式下,gcc-8.1和8.2也会拒绝它:structFoo{explicitFoo(intptr);};templatestructBar{operatorT()const;templateexplicitoperatorT2()const;};Foofoo(Barx){return(Foo)x;}接受它的编译器选择模板化显式转换函数Bar::operatorT2().拒绝它的编译器同意以下之间存

c++ - 在安全关键系统中测试 C++17

我目前正在考虑安全关键软件(DO-178CDAL-D)中的C++和编码标准的定义。我在看MISRAC++,它又有10年的历史了,它错过了所有C++11…17的特性。虽然在安全性方面保持保守通常不是一个坏主意,但新的语言功能可能对安全性有益。在审查期间,人们必须争论您做出某些决定的原因。人们总是会争辩说,新的语言特性使代码更清晰……因此关于误解的错误更少;特别是如果编译器能够测试和验证您的假设。但很难找到比“让事情更清晰”更突出安全方面的语言特征。现代C++的哪些方面真正有助于安全?我正在建立一个小型练习项目来测试这些想法,目前完全专注于“让编译器检查你的假设”。例如,我们刚刚开始使用[

c++ - 重载运算符 && 和 || 短路在 C++17 中

我阅读了http://en.cppreference.com/w/cpp/language/operators:Thebooleanlogicoperators,operator&&andoperator||Unlikethebuilt-inversions,theoverloadsdonotsequencetheirleftoperandbeforetherightone,and(untilC++17)cannotimplementshort-circuitevaluation.(我的重点)。找不到支持短路的C++17的任何资源或代码示例对于运算符&&和运算符||。它与C++17参数