草庐IT

c++ - SFINAE:检测类是否具有自由函数

有没有办法使用SFINAE检测给定类的自由函数是否重载?基本上,我有以下解决方案:structhas_no_f{};structhas_f{};voidf(has_fconst&x){}templateenable_if::value,int>::typecall(Tconst&){std::coutdisable_if::value,int>::typecall(Tconst&){std::cout简单地重载call是行不通的,因为实际上有很多foo和bar类型以及call函数不知道它们(基本上call在a中,用户提供他们自己的类型)。我不能使用C++0x,我需要一个适用于所有现代编

c++ - SFINAE,演绎与实例化

此代码无法在大多数编译器中编译,但起初我直觉地希望SFINAE能保护我:typedefvoid(*A)();templatestructa_metafun{typedeftypenameT::typetype;};templatetypenamea_metafun::typef(T){}templatevoidf(T(*)()){}intmain(){f(A());}我至少可以通过两种方式解决问题:将“metafun”f()的定义更改为:templatetypenameT::typef(T){}定义a_metafun这样它就可以分析T并且有一个类型ifT有一个,如果没有,则没有……但是

C++ GCC 为什么这段 sfinae 代码可以用 GCC 4.7 编译,但不能用 4.8 编译?

我喜欢在模板类中使用本地类来执行类似“staticif”的构造。但是我遇到了gcc4.8不想编译我的代码的问题。但是4.7可以。这个例子:#include#include#includeusingnamespacestd;structA{voidprintA(){coutstructTest{voidprint(){structIfA{constexprIfA(T&value):value(value){}T&value;voidprint(){value.printA();}};structIfB{constexprIfB(T&value):value(value){}T&value

c++ - 无限递归 `enable_if`

在尝试为另一种类型T编写wrapper类型时,我遇到了一个相当令人讨厌的问题:我想定义一些二元运算符(例如+)将wrapper上的任何操作转发给底层类型,但我需要这些运算符接受任何涉及wrapper的潜在组合:wrapper()+wrapper()wrapper()+T()T()+wrapper()天真的方法涉及直接编写所有潜在的重载。但我不喜欢编写重复的代码并且想要更多的挑战,所以我选择使用一个非常通用的模板来实现它并使用enable_if限制潜在的类型。我的尝试显示在问题的底部(抱歉,这是我能想到的最小值)。问题是它会遇到无限递归错误:为了评估test()+test(),编译会查看

c++ - g++ 和 clang++ SFINAE 和 SFINAE 失败的不同行为

C++11专家的几个问题。我正在与SFINAE打交道,我遇到了一个奇怪的情况,其中g++(4.9.2)和clang++(3.5.0)的行为不同。我准备了以下示例代码。很抱歉,我无法做到更简洁。#include#include#include#includetemplateclassfoo{private:templateusingenableIfIsInt=typenamestd::enable_if::value,R>::type;public:foo(){}templateenableIfIsIntbar(){std::cout).name()fl;foofi;fl.bar();f

c++ - 为什么 SFINAE 在更改类模板特化的位置时会搞砸?这是 C++ 错误吗?

以下代码给出了预期的编译器错误(Demo):1templatestructRange;23template>structUnique;4templatestructUnique1)>>{typedefchartype[1];};5templatestructUnique2)>>{typedefchartype[2];};67Unique::typeo1;8Unique::typeo2;现在,如果我交换第5行和第7行。然后是没有编译器错误!!Demo.5Unique::typeo1;7templatestructUnique2)>>{typedefchartype[2];};对于o1,没

c++ - std::string 的 type_traits 段错误

从UsingSFINAEtocheckforglobaloperator收集信息和templates,decltypeandnon-classtypes,我得到了以下代码:http://ideone.com/sEQc87基本上,我将两个问题的代码结合起来,如果它有ostream声明,则调用print函数,否则调用to_string方法。摘自问题1namespacehas_insertion_operator_impl{typedefcharno;typedefcharyes[2];structany_t{templateany_t(Tconst&);};nooperatorstruct

c++ - 如何 SFINAE 输出非容器参数

我有一个模板函数,我只想为标准容器(或与标准容器兼容的容器,它至少提供一个begin()成员函数)启用。我正在通过以下方式排除非容器SFINAE:templatetypenameContainer::value_typef(constContainer&c,typenamestd::enable_if::value>::type*=nullptr){//implementationhere}std::is_same和decltype看起来不太优雅。有更好的方法吗?PS:我这里需要SFINAE因为我有不同的过载templatef(constEigen::MatrixBase&A)每当我尝

c++ - 在 C++ 中检测运算符是否存在和可调用(考虑 static_asserts)

给定2种类型T和U我想检测是否可以调用operator*在对象之间(即是否可以写t*u,其中t是T类型,u是U类型)我正在使用c++detectionidiom但由于它在我的编译器中还不可用,所以我自己实现了它structnonesuch{nonesuch()=delete;~nonesuch()=delete;nonesuch(nonesuchconst&)=delete;voidoperator=(nonesuchconst&)=delete;};namespacedetail{templateclassOp,class...Args>structdetector{usingval

c++ - sfinae 远离破坏者

我正在实现与std::vector非常相似的东西,但在堆栈上使用数组而不是内存分配。d-tor调用一个使用SFINAE的函数。如果value_type是POD,则函数体为空。如果value_type是普通类,例如std::string,则函数有主体并正确销毁所有数据。现在,我希望能够将这个新的std::vector用作constexpr。然而,即使c-tor被声明为constexpr,代码也无法编译,因为该类具有非平凡的d-tor。部分代码如下:templateclassSmallVector{constexprSmallVector()=default;~SmallVector(){