jjzjj

typeName

全部标签

c++ - 要将嵌套类中定义的静态模板函数声明为兄弟嵌套类中的友元,必须做什么?

在Linux上使用GCC4.8.2,我想授予工厂方法Create()访问类C的私有(private)构造函数的权限,但在尝试声明时出现“错误:‘Create’未在此范围内声明”一个专业的friend。如何在不向B::Create()的所有类型开放声明的情况下使其工作?templateclassA{public:classB;templateclassC;};templateclassA::B{public:templatestaticvoidCreate();};templatetemplateclassA::C{C()=default;friendvoidB::Create();};

c++ - 从函数调用推导模板参数包

我有以下代码,其中有一个模板类和其中的一个类型,我想在单独的模板函数中使用它。templatestructMyClass{enumSomeEnum{value0=-1};};templatestructOtherClass{};templateTcheck(typenameMyClass::SomeEnumvalue){OtherClassobj;Tresult;//calculateresultfromobj;returnresult;}intmain(){autovalue=MyClass::value0;//...intt=check(value);}我相信编译器能够从函数调用中

c++ - 模板仿函数与函数

我一直在查看一些Boost源代码并注意到它们通过使用仿函数而不是普通函数来实现模板化函数?这是有原因的吗?例如:templatestructfunctor{Baroperator()(constFoo&foo){returnfoo.as_bar();}};相对于:templateBarfunc(constFoo&foo){returnfoo.as_bar();}我能想到的唯一优点是它允许类继承函数? 最佳答案 主要有两个原因:首先,正如pythonic隐喻所指出的,偏特化只对类有效,对函数无效。请注意,函数通常可以使用重载来克服这个

c++ - Boost fusion 使用 lambda fold 参数包

在下面的代码中(C++14,C++17中没有“fold”),我试图在编译时使用boostfusionfold、参数包自动计算类字段的固定偏移量和一个lambda。不幸的是,这会导致编译时错误......是否可以这样做?[编辑:其他事情也困扰着我:这不是我想要的。我希望ControlledLayout2的_size在编译时可用(这就是我将其设为静态的原因),而不仅仅是在调用构造函数时可用]templatestructField2{typedefT_type;staticconstuint32_t_size;staticuint32_t_offset;};templateconstuint

c++ - 具有单个参数模板的成员函数包装器?

我做了一个模板函数,它接受一个成员函数作为参数。但是,由于类必须先声明才能用作成员函数参数的一部分,因此我必须将其作为一个单独的参数:templatefunctionmethodWrap(){}这意味着当显式实例化模板时(我希望这些包装器在编译时生成,而不是将成员指针作为参数传递)我在使用它时必须输入两次:functionsomeFunc=wrapMethod();为什么我不能写这样的东西:templatefunctionmethodWrap(){}并让它捕获C的类型及其成员函数指针,而无需键入两次SomeClass?或者为什么我不能将它包装在一个将C声明为“自由变量”然后具有执行推导

c++ - join_rows() 函数的正确类型是什么?

我写了一个函数joinstherowsoftwo2Darrays:templateArrayjoin_rows(constArrayBase&A,constArrayBase&B){ArrayC(A.rows(),A.cols()+B.cols());C我想写一个更通用的函数,可以连接两个以上的数组。它应该能够与任何可迭代的容器一起工作,例如。std::list或std::vector,所以我会使用模板参数表。我可以很容易地用两个for循环来调整函数体,这不是问题,我只是在努力弄清楚这样一个函数的正确类型是什么。(ps。我什至不确定我上面的代码是否有最好的类型,但它似乎可以完成工作)

c++ - 解决静态断言中不完整的类型

当表达式依赖于类类型本身时,有没有办法在类内部进行static_assert?也许延迟评估直到类型完成或模板实例化之后?示例代码:#includetemplatestructTest{Tx=0;//makenon-trivialstatic_assert(std::is_trivial>::value,"");};intmain(){//wouldlikestaticassertfailure,insteadget'incompletetype'errorTesttest1;Testtest2;return0;} 最佳答案 这是一个

C++:为什么禁止递归模板化别名?

为什么编译失败:templateusingvec=vector>;templateusingvec=T;虽然只是将它嵌套到一个结构中就可以了:templatestructfoo{usingvec=vector::vec>;};templatestructfoo{usingvec=T;};如果您可以用更冗长的结构替换它,那么禁止在别名中递归的理由是什么?参见:https://godbolt.org/g/YtyhvL 最佳答案 Whatistherationaleforforbiddingrecursioninaliasesifyouc

c++ - 类(Class)友元和继承问题

我觉得我在这里遗漏了一些明显的东西,但我有一个类将另一个类声明为友元,但似乎无法访问其私有(private)成员。我已将问题最小化如下:Widget.hpp:templateclassFoo;templateclassWidgetBase{protected:T*ptr;public:WidgetBase(T*ptr):ptr{ptr}{}virtualvoidf()=0;};templateclassWidgetDerived:publicWidgetBase::Bar,T>{public:usingWidgetBase::Bar,T>::WidgetBase;usingWidget

c++ - 类模板,在定义中引用它自己的类型

我想知道关于标准的以下情况,visualstudio2017和GCC哪个是正确的。问题是在类模板second中,visualstudio中的标识符“second”总是指具体类型,但在gcc中它似乎是上下文相关的。GCC示例templatestructtype_list{};templatetypenametmpl>structtmpl_c{templateusingtype=tmpl;};templatestructtemplate_of;templatetypenametmpl,typename...Ts>structtemplate_of>{usingtype=tmpl_c;};t