草庐IT

-std=gnu99

全部标签

c++ - std::ref 在此函数中有什么用?

为什么人们更喜欢调用std::ref而不是根本不调用它?templateFfor_each_arg(Ff,Ts&&...a){return(void)initializer_list{(ref(f)((Ts&&)a),0)...},f;//whynotreturn(void)initializer_list{(f((Ts&&)a),0)...},f;} 最佳答案 std::reference_wrapper::operator()在某些情况下,执行一些超出直接函数调用的“魔法”。其效果指定为(引用N4296[refwrap.invo

c++ - 如何专门化 std::begin?

我正在尝试为自定义容器专门化std::begin。我这样做是因为我想对容器使用基于范围的for。这是我的:classstackiterator{…};classstack{…};#includetemplatestackiteratorstd::begin(stack&S){returnS.GetBottom();}我在begin特化的定义中遇到以下错误:Nofunctiontemplatematchesfunctiontemplatespecialization'begin'我做错了什么? 最佳答案 I'mtryingtospec

c++ - std::apply 和常量表达式?

我在Wandbox中尝试了以下代码:#include#include#include#include#include#includeintmain(){constexprstd::arraystr{"123456789"};constexprautofoo=std::apply([](auto...args)constexpr{std::integer_sequence{};},str);std::cout编译器告诉我args...不是常量表达式。怎么了? 最佳答案 函数参数不能被标记为constexpr。因此,您不能在需要常量表达

c++ - 如果为 false,则 std::is_member_function_pointer 不编译

我在寻找什么:我有一个模板化类,如果该类具有所需的函数,我想调用一个函数,例如:templatedo_something(){ifconstexpr(std::is_member_function_pointer::value){this->_t->x();//_tistypeofT*}}会发生什么:如果T没有带来函数,编译器就不会编译。小例子:#include#includeclassFoo{public:voidx(){}};classBar{};intmain(){std::cout::value::value编译器说:is_member_function_pointer.cpp

c++ - 如何在不调整大小的情况下从 std::vector 中删除元素

iteratorerase(iteratorposition);iteratorerase(iteratorfirst,iteratorlast);EraseelementsRemovesfromthevectorcontainereitherasingleelement(position)orarangeofelements([first,last)).Thiseffectivelyreducesthevectorsizebythenumberofelementsremoved,callingeachelement'sdestructorbefore.和:removeRemovesa

c++ - std::vector 插入对象的拷贝或引用?

假设我有一个动态分配的对象。如果我将其放入STLvector中,是否会将引用插入vector或该对象的拷贝中?这是一个普遍的问题。例如:classvec{vectorvec;voidaddToVec(obja){//insertaintovec}.........}obj*a=newobj;vec*v=newvec;vec.addToVec(a);如果我删除v,是否也会反对骰子? 最佳答案 willareferencebeinsertedintothevectororacopyofthisobject?复制(这意味着您的class应

c++ - bind 不是 std 的成员

我正在使用带有minwg编译器的netbeans7.2.1。尝试构建应用程序时收到以下错误消息:error:'function'innamespace'std'doesnotnameatypeerror:'bind'isnotamemberof'std'尽管我在文件的开头包含了functional.h,并且我正在使用以下形式的“函数”和“绑定(bind)”:std::function和std::bind问题出在哪里?它在编译器中还是缺少某些东西?我记得我在visualstudio2010上成功编译并运行了相同的应用程序。 最佳答案

c++ - std::move 在类模板中的构造函数初始值设定项列表中

我有一个这样的模板:templatestructfoo{Tm_t;foo(Tt):m_t(t){}};问题是我想为T支持小型/常规类型和大型类型(如矩阵)。你推荐我这样写构造函数初始化列表吗foo(Tt):m_t(std::move(t)){}并要求T类型始终支持move构造,即使对于较小的类型也是如此?有没有更好的方法? 最佳答案 andrequirethatthetypeTalwayssupportmoveconstruction,evenforsmallertypes?任何可复制构造的类型也可move构造。在这些情况下move

c++ - 使用 'auto' 和 std::minmax 观察奇怪的行为

我在SUSEEnterpriseLinux11上使用GCC4.7.2和Boost1.58.0。我有以下代码片段,它基本上通过多边形列表来计算它们的长度/宽度。在std::minmax函数中使用“auto”关键字时,我看到了奇怪的输出。为了进行比较,我还声明了第二个变量,其中明确声明了类型(即dim与dim1)。namespacegtl=boost::polygon;typedefgtl::polygon_90_dataLayoutPolygon;typedefgtl::rectangle_dataLayoutRectangle;staticLayoutFeatureVeccalc_st

c++ - 可变参数模板和 std::bind

给定以下模板化函数,我如何更改它以利用可变参数模板?也就是说,用可变参数代替P1和P2来代替std::bind占位符?目前我每个元数都有这些函数之一,零元数没有P参数,直到元数9有P1到P9参数。如果可能的话,我希望将其合并为一个函数。templatevoidAttach(R(T::*f)(P1,P2),Up){AttachInternal(p,std::bind(f,p.get(),std::placeholders::_1,std::placeholders::_2));} 最佳答案 您可以(部分)专攻std::is_place