草庐IT

Metaprogramming

全部标签

c++ - 混合 typedef 和 CRTP?

考虑以下示例:#include#include#includetemplateclassCrtp>classBase{public:typedefintvalue;//f1:OK//Expectedresult:casts4.2toBase::valuevaluef1(){return4.2;}//f2:NOTOK//Expectedresult:casts4.2toCrtp::value//Butf2doesnotcompile:notypenamed'value'//in'classDerived'typenameCrtp::valuef2(){return4.2;}};templ

c++ - 是否可以在编译时检查类型是否派生自模板的某些实例化?

我想编写一个模板函数,如果传递的类型是从另一个类的任何模板实例派生的,则它以一种方式运行,否则以另一种方式运行。我认为下面的代码捕捉到了我想做的事情。不幸的是,Caller为double和Derived打印“generic”。#includetemplatestructBase{};structDerived:publicBase{};templatevoidFoo(constT&){std::coutvoidFoo(constBase&){std::cout"voidCaller(constT&t){Foo(t);}intmain(){doublex;Caller(x);Derive

c++ - 折叠任意多个可变参数包

我正在阅读EricNiebler的post在他的小型元编程库上。在尝试实现他遗漏/列为挑战的部分时,我只剩下以下transform的实现:templateusingmeta_apply=typenameF::templateapply;templatestructtypelist_transform;//unarytemplatestructtypelist_transform,F>{usingtype=typelist...>;};//binarytemplatestructtypelist_transform,typelist,F>{usingtype=typelist...>;}

c++ - 在没有 SFINAE 的情况下启用类方法

我正在寻找一种无需SFINAE即可启用类方法的方法,可能是通过继承。Imworkingonanimprovedversionofstd::function(带有operator()的仿函数类)哪个限定符(const,volatile)取决于它的模板参数,例如:myfunctor提供operator()()myfunctor提供operator()()constmyfunctor提供operator()()volatile等等。我不能使用SFINAE解决这个问题的主要原因是,如果我使用SFINAE,则operator()需要像这样模板化:templateautooperator()(Ar

c++ - 处理类和类的模板函数*

下面的代码允许我模板化一个函数接受一个参数,该参数是指向Box对象的三种不同指针类型之一的vector:conststd::vector>&conststd::vector>&conststd::vector&有没有办法扩展它以支持:constvector&constvector>也许有一些boost?#include#includeclassBox{public:Box(unsignedintid,unsignedintside):id(id),side(side){}intvolume(){returnside*side*side;}unsignedintid;unsignedin

c++ - 为什么这个用于检测类型 T 是否具有 void operator(EDT const&) 的 C++ 特性会失败?

我正在尝试使用SFINAE来检测作为模板参数T传递的类型是否具有T::operator()(Pconst&),其中P也是模板参数。我在MemberDetectorIdiom的这个例子之后为我的解决方案建模不幸的是,我无法让它为operator()工作,即使我可以让它为普通方法工作。下面是一些演示我面临的问题的示例代码:#include#include#include#includeusingnamespacestd;structhas{voidoperator()(intconst&);};structhasNot1{voidoperator()(int);};structhasNot

c++ - 我可以检测在编译时使用了哪个标签分派(dispatch)重载吗?

假设我有一个仿函数,它使用标签分派(dispatch)从函数的多个实现中进行选择,如下所示://baseclassforalltags,indicatingthe"default"implementationstructtag_base{};//subclassesfortagsthatmightselectadifferentimplementationstructtag1:tag_base{};structtag2:tag1{};structtag3:tag2{};structfunc{voidoperator()(tag_base){}voidoperator()(tag3){}

c++ - 检测 SFINAE 的 POD 类型的第一个成员

给定一些与此类似的POD结构:structStandardHeader{uint32_tfield1;uint32_tfield2;};structTypeA{StandardHeaderHeader;uint8_tfield3;};structTypeB{StandardHeaderHeader;uint16_tfield4;};我想写一个类型特征(或类似的东西,最终可以在static_assert和std::enable_if中使用,或者以其他方式禁用模板化方法),它可以检测StandardHeader的存在。字段作为标准布局类型的第一个成员——即。这样reinterpret_ca

c++ - 根据模板参数调用不同的函数c++

我想要这样的东西classA{public:Array&operator()(){...}};classB{public:Element&operator[](inti){...}};templateclassexecute{public:output_type=operator()(T&t){if(T==A)Arrayout=T()();else{Arrayres;for(inti=0;i这里有两个问题:在执行运算符()中替换if-else的元函数执行运算符()的返回类型 最佳答案 只专门化模板类。templateclassexe

c++ - 如何从成员函数模板类型签名中删除 const?

我正在研究一些C++类型系统的东西,但我在从成员函数中删除const-ness以用于函数特征类时遇到问题。真正麻烦的是,这在G++中运行良好,但MSVC10无法正确处理部分特化,我不知道这些编译器中的一个是否真的存在错误。这里的问题是,以我可以获得函数类型签名的方式从成员函数中删除const限定符的正确方法是什么?采用以下代码示例:#includetemplatestructRemovePointer{typedefTType;};templatestructRemovePointer{typedefTType;};templatestructRemovePointer{typedef