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
以下代码编译良好: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::
见,例如,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++17中,我们将有可能返回不可移动(包括不可复制)类型,例如std::mutex,这可以被认为是保证返回值优化(RVO):Guaranteedcopyelisionthroughsimplifiedvaluecategories:structnocopy{nocopy(nocopy&)=delete;nocopy()=default;};autogetRVO(){returnnocopy();}我们也会有structuredbindings,允许:tuplef();auto[x,y,z]=f();或者(这里也使用我对特性templateargumentdeductionforco
我阅读了C++17标准$8.5.7.4:TheexpressionE1issequencedbeforetheexpressionE2.用于移位运算符。还有cppreference第19条规定:InashiftoperatorexpressionE1E2,everyvaluecomputationandside-effectofE1issequencedbeforeeveryvaluecomputationandsideeffectofE2但是当我尝试使用gcc7.3.0或clang6.0.0编译以下代码时#includeusingnamespacestd;intmain(){inti
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().拒绝它的编译器同意以下之间存
我目前正在考虑安全关键软件(DO-178CDAL-D)中的C++和编码标准的定义。我在看MISRAC++,它又有10年的历史了,它错过了所有C++11…17的特性。虽然在安全性方面保持保守通常不是一个坏主意,但新的语言功能可能对安全性有益。在审查期间,人们必须争论您做出某些决定的原因。人们总是会争辩说,新的语言特性使代码更清晰……因此关于误解的错误更少;特别是如果编译器能够测试和验证您的假设。但很难找到比“让事情更清晰”更突出安全方面的语言特征。现代C++的哪些方面真正有助于安全?我正在建立一个小型练习项目来测试这些想法,目前完全专注于“让编译器检查你的假设”。例如,我们刚刚开始使用[
我阅读了http://en.cppreference.com/w/cpp/language/operators:Thebooleanlogicoperators,operator&&andoperator||Unlikethebuilt-inversions,theoverloadsdonotsequencetheirleftoperandbeforetherightone,and(untilC++17)cannotimplementshort-circuitevaluation.(我的重点)。找不到支持短路的C++17的任何资源或代码示例对于运算符&&和运算符||。它与C++17参数
我们每个人(可能)都有儿时写作的梦想:switch(my_std_string){case"foo":do_stuff();break;case"bar":do_other_stuff();break;default:just_give_up();}但这是不可能的,正如过去(2009年)对这个问题的回答中所解释的那样:Whytheswitchstatementcannotbeappliedonstrings?从那时起,我们见证了C++11的出现,它让我们走得更远:switch(my_hash::hash(my_std_string)){case"foo"_hash:do_stuff()
我在Ubuntu16.04上使用gcc7.2,我需要使用来自C++17的新文件系统库。即使确实有一个名为experimental/filesystem的库,我也不能使用它的任何成员。例如,当我尝试编译这个文件时:#include#include#includeusingnamespacestd;namespacefs=std::experimental::filesystem::v1;intmain(){fs::pathp1="/usr/share/";}我收到如下所示的编译错误:$g++-7test.cpp-std=c++17/tmp/ccfsMnlG.o:Infunction`st