草庐IT

enable-mirrors

全部标签

c++ - 为什么编译器说 : 'enable_if' cannot be used to disable this declaration

templateusingEnable_if=typenamestd::enable_if::type;classDegree;templateconstexprinlineboolIs_Degree(){returnstd::is_base_of::value;}classDegree{public:std::size_tinDeg=0;};templateclassVertex:publicSatellite{public:explicitVertex(intnum):n(num){}private:std::size_tn;};templateclassEdge{public:/

c++ - 使用 enable_if 专门化模板方法

我正在编写一个模板类,它存储一个std::function以便稍后调用它。这是简化的代码:templatestructTest{voidcall(Ttype){function(type);}std::functionfunction;};问题是这个模板不能为void类型编译,因为voidcall(voidtype)变得未定义。将它专门用于void类型并不能缓解问题,因为templatevoidTest::call(void){function();}仍然与call(TType)的声明不兼容。因此,利用C++11的新特性,我尝试了std::enable_if:typenamestd::

用于非类型模板参数的 c++ enable_if

我对部分模板特化有点困惑...我有一些代码依赖于算术数据类型T和小整数DIM。我希望能够为不同的DIM值指定不同的类方法。使用部分模板特化的不可能让我探索enable_if。这正是我所需要的……除了我希望它返回一个数字而不是一个类型。我怎样才能做到这一点?下面的代码应该说明我想要什么。#include#include#includetemplateclassfoo{public:Tfunction();};templateTfoo::value>::function(){//dosomethingreturn1.0;}templateTfoo::value>::function(){/

c++ - 如何将 enable_if 用于互斥的非成员函数模板?

我正在尝试编写非成员运算符函数模板,例如:#includetemplateclassMyType;templateautooperator==(MyTypeconst&l,MyTypeconst&r)->decltype(std::declval()==std::declval()){/*...*/}但是当我尝试处理l和r的长度不同时:template::type>autooperator==(MyTypeconst&l,MyTypeconst&r)->decltype(std::declval()==std::declval()){/*...*/}templateLu)>::type

c++ - 迭代器或指针的 std::enable_if 或 SFINAE

我想为MyClass编写一个带有参数的构造函数,并且我希望仅当参数是一个pointer或iterator(具有iterator_traits的东西)。如何实现? 最佳答案 遗憾的是,没有标准的方法来检测类是否为Iterator模型。最简单的检查是*it和++it在语法上都是有效的;您可以使用标准SFINAE技术执行此操作:template(),void(),++std::declval(),void())>MyClass(T);考虑到24.2.2:2中的Iterator要求:templatetypenamestd::enable_i

c++ - enable_if 有条件地包含成员函数

我有一个模板类,它的类型是迭代器。我想根据模板参数的iterator_category启用/禁用特定成员函数。特别是,我想启用operator--如果模板参数是双向迭代器。我的尝试是这样的:typenamestd::enable_if::value,MyType&>::typeoperator--(){//doworkreturn*this;}Clang告诉我(大致):error:notypenamed'type'in'std::__1::enable_if';'enable_if'cannotbeusedtodisablethisdeclaration有没有办法完成我正在尝试的事情?

c++ - 是否可以将 'enable_if' 和 'is_same' 与可变函数模板一起使用?

这两个非可变函数模板编译:templatetypenamestd::enable_if::value,void>::typetestFunction(Ta,Ub){std::couttypenamestd::enable_if::value,void>::typetestFunction(Ta,Ub){std::cout但是,类似的可变参数模板无法编译:templatetypenamestd::enable_if::value,void>::typetestFunction(Ta,U...bs){std::couttypenamestd::enable_if::value,void>:

c++ - 如何在转换运算符中使用 std::enable_if?

基本上我希望我的范围类型可以从Range隐式转换至Range.std::enable_if似乎是不可能的,因为该函数不带任何参数并且没有返回值。解决办法是什么?这基本上是我尝试过的:templateclassRange{T*begin_;T*end_;public:Range(T*begin,T*end):begin_{begin},end_{end}{}templateRange(T(&a)[N]):begin_{static_cast(&a[0])},end_{static_cast(&a[N-1])}{}T*Begin(){returnbegin_;}T*End(){return

c++ - 如何根据模板类型使用 std::enable_if 来启用或禁用构造函数?

我有以下模板化对象:templatestructresult{//Iwanttoenablethesetwoconstructorsonlyiftype_1!=type_2result(type_1f):foo{f}{}result(type_2b):bar{b}{}//Iwanttoenablethisconstructoronlyiftype_1==type_2result(type_1f,type_2b):foo{f},bar{b}{}//Othermemberfunctionsremoved.type_1foo;type_2bar;};如何使用std::enable_if根据需

c++ - --enable-pic 在 Visual Studio 中等效

在VisualStudioC++项目中什么设置等同于*nix--enable-pic开关,即./configure--enable-pic 最佳答案 没有,因为不需要。http://en.wikipedia.org/wiki/Position-independent_code:“MicrosoftWindowsDLL不是Unix意义上的共享库,并且不使用与位置无关的代码” 关于c++---enable-pic在VisualStudio中等效,我们在StackOverflow上找到一个类似