草庐IT

C++14:具有通用 std::function 作为类成员的通用 lambda

考虑这个伪片段:classSomeClass{public:SomeClass(){if(true){fooCall=[](autoa){coutstd::functionfooCall;};我想要的是一个存储通用lambda的类成员fooCall,它又在构造函数中赋值。编译器提示fooCall不能成为模板化数据成员。有没有关于如何在类中存储通用lambda的简单解决方案? 最佳答案 您无法在运行时在两个通用lambda之间进行选择,因为您没有具体的签名来进行类型删除。如果您可以在编译时做出决定,您可以将类本身模板化:templat

c++ - 英特尔 C++ 编译器 (icpc 14.0) : "a derived class is not allowed here"

我在Linux上使用ICC14.0.2。此代码片段使用GCC和CLang编译,但不使用ICC:templatestructA;templatestructA{templatestructNested{};};templatestructA:publicA{};templatestructA::Nested;//explicitinstantiation尝试用三个编译器编译:$g++-c-std=c++11testcase.cc$clang++-c-std=c++11testcase.cc$icpc-c-std=c++11testcase.cctestcase.cc(17):error:

c++ - C++14 中的返回类型推导

我刚刚阅读了C++14中可用的名为“返回类型推导”的新功能,我对该类型的函数中的递归有疑问。我了解到该函数中的第一个返回必须允许推断返回类型。Wiki提供的示例完全符合该规则。autoCorrect(inti){if(i==1)returni;//returntypededucedasintelsereturnCorrect(i-1)+i;//oktocallitnow}autoWrong(inti){if(i!=1)returnWrong(i-1)+i;//Toosoontocallthis.Nopriorreturnstatement.elsereturni;//returntyp

构造函数中的 C++14 constexpr union 条件初始化

我想根据参数选择在构造函数中初始化的union成员。以下是一个有效的示例:structA{union{inti;floatf;};A(doubled,boolisint){if(isint)new(&i)int(d);elsenew(&f)float(d);}};当我使用int和float时,目标是使用其他更复杂的类型(但在C++14union中仍然允许),因此使用placement-new(而不是分配)。问题是这个构造函数不能是constexpr因为在constexpr方法中不允许放置新的。有没有办法解决这个问题(除了使isint参数成为正式类型系统的一部分)?某种类型的条件初始化器

C++14 我应该多久使用一次 constexpr?

我一直在读一本关于C++14/11的书。我刚读完有关constexpr关键字的一章。我知道它的用途,但我应该多久使用一次constexpr?我是否应该在我知道永远不会用于创建contstexpr对象的类的代码中使用它?(以防万一,因为它不需要我任何费用,对吧?) 最佳答案 C++14HowoftenshouldIuseconstexpr?在ScottMeyer's的第15项(尽可能使用constexpr)中有广泛的讨论。书EffectiveModernC++.这一项的概要是应该尽可能使用constexpr,因为constexpr函数

c++ - 本地类规则是否与 c++14 返回类型推导一致?

在阅读C++14的这一部分时(afreedraftN4141,closesttoC++14):9.8Localclassdeclarations[class.local][..]Thenameofalocalclassislocaltoitsenclosingscope.[..]Declarationsinalocalclassshallnotodr-use(3.2)avariablewithautomaticstoragedurationfromanenclosingscope.[Example://[..]voidf(){staticints;intx;//[..]structlo

c++ - 据我所知,根据 C++14 中的 §5.19/3 和 §5.19/2,这段代码不应该编译

但它在gcc4.9.0中编译。参见liveexample:#includestructA{constexprA():i(5){}int&&f(){returnstd::move(i);}inti;}a;A&&f(A&a){returnstd::move(a);}intmain(){Aa;intb[a.f()]{0,1,2,3,4};std::cout从§5.19/3我们有:Anintegralconstantexpressionisanexpressionofintegralorunscopedenumerationtype,implicitlyconvertedtoaprvalue,

c++ - 当函数返回类型为 bool 时,为什么不能在 C++14 中返回共享指针?

我正在使用g++并编写一个简单的函数:#includestd::shared_ptrptr;boolfails_compiling(){returnptr;}从我在界面中看到的内容来看,shared_ptr实现包括一个bool运算符,我什至可以像这样应用快速修复:returnstatic_cast(ptr);现在可以编译了。为什么返回算法不像if()和while()那样尝试自动转换为bool? 最佳答案 如果你结账std::shared_ptr的bool转换运算符,您会看到它被声明为:explicitoperatorbool()co

c++ - 这个 constexpr 虚函数技术是否违反了任何 C++11/C++14 规则?

前几天我在阅读C++文档时注意到,虽然字面量类型不能有虚成员,但这并不妨碍它们实现虚成员。或者至少我是这么理解的。这是我一直在玩的一段代码:#include//Someforwarddeclarations:enumclassliteral_id;structliteral_base;structliteral_a;structliteral_b;//Nowsomedefinitions:enumclassliteral_id{a,b};structliteral_base{virtualliteral_idmethod()constnoexcept=0;};structliteral

C++11/14/17,GCC 7 与 GCC 8 : Name lookup for friend class templates

我想弄清楚以下代码在GCC7中是否有效,但在GCC8.1中无效。代码的作用是:定义(并转发声明)类模板MyGoodFriend(在全局命名空间中)在inner命名空间中定义一个类模板Befriended使MyGoodFriend的所有特化成为Befriended的friend有问题的部分是templatefriendclassMyGoodFriend;我明白问题是什么了。GCC8.1要求我在friend声明中使用完全限定名称::MyGoodFriend-然而,GCC7对MyGoodFriend很满意。这是代码:templateclassMyGoodFriend;namespaceinn