草庐IT

c++ - SFINAE 没有避免模棱两可的调用

编译这段代码:#includetemplatestructTestClass{template::type=0>voiddoAction(){std::cout();\n";}};structHostClass:publicTestClass,publicTestClass{};intmain(intargc,constchar*argv[]){HostClasshostClass;hostClass.doAction();hostClass.doAction();return0;}导致不明确的调用错误,因为doAction都在TestClass中和TestClass父类。main.c

c++ - SFINAE成员函数存在性测试问题

我有这个成员函数测试:templatestructhas_member{templatestatictrue_typef(decltype(declval().member())*);templatestaticfalse_typef(...);staticconstboolvalue=decltype(f(0))::value;};如果存在具有给定名称的成员函数,并且该函数具有不带参数的重载,则它的计算结果为true。对于此类函数以及在STL容器的情况下,它可以正常工作,但元素访问函数(前面、后面等)除外,在这些函数中它总是计算为false。这是为什么呢?我有mingwg++4.7。

c++ - 右值或左值 (const) 引用参数

我想通过r-或l-值(const)引用将参数(某种具体类型,比如int)传递给成员函数。我的解决方案是:#include#includestructF{usingdesired_parameter_type=int;template::type,desired_parameter_type>::value>::type>voidoperator()(X&&x)const{//orevenstatic_assert(std::is_same::type,desired_parameter_type>::value,"");std::forward(x);//somethinguseful

c++ - 使用 enable_if 禁用模板类的模板构造函数

当模板构造函数的参数类型与类型“MyClass匹配时,我试图使用std::enable_if禁用模板类的模板构造函数"这样我就可以使用我的其他构造函数,它允许我用另一个模板的类初始化当前模板的类。templateclassMyClass{public:MyClass(){data.fill(static_cast(0));}template//iwanttodisablethisifArgs=MyClassMyClass(Args&&...args):data{std::forward(args)...}{}templateMyClass(constMyclass&other_size

c++ - 检查类是否在继承层次结构中明确定义成员类型

我有一组使用成员typedefNext链接的类,如下:classY;classZ;classX{public:typedefYNext;};classY{public:typedefZNext;};classZ{};我需要一种方法来获取链的最终类,从链的任何类开始。感谢acceptedanswerofthispost,我写了下面的代码://cond_type::type//selectstype'Then'if'Condition'istrue,ortype'Else'otherwisetemplatestructcond_type{typedefThentype;};template

c++ - 无法让 SFINAE 工作

这是我第一次尝试SFINAE:#include#includestructC1{usingT=int;};structC2{usingT=void;};//ForclassesthatdeclareT=inttemplatevoidf(C&c,std::enable_if::value,int>::type=0){std::coutvoidf(C&c,std::enable_if::value,int>::type=0){std::cout编译器(gcc4.8.2)提示:‘std::enable_if::value),int>::type’isnotatype我做错了什么?

c++ - 带有 std::enable_if 和 std::is_default_constructible 的 SFINAE 用于 libc++ 中的不完整类型

我刚刚在使用SFINAE检测模板类型是否默认可构造时观察到libc++的一个奇怪问题。以下是我能够想出的一个最小示例:#include#includetemplatestructDummy;templatestructDummy{};templatestructhas_dummy:std::false_type{};templatestructhas_dummy>::value>>:std::true_type{};intmain(){std::cout{}(){}()它编译并输出预期的行true和false使用libstdc++使用g++或clang++编译时.但是,当我尝试使用li

c++ - 为什么 SFINAE (enable_if) 从类定义内部工作而不是从外部工作

过去几个小时我一直在努力解决一个非常奇怪的问题(在用SFINAE解决了5-6个其他问题之后,因为我是新手)。基本上在下面的代码中,我想让f()为所有可能的模板实例化工作,但是g()仅在N==2:#include#includetemplateclassA{public:voidf(void);voidg(void);};templateinlinevoidA::f(){std::cout::type*=nullptr>inlinevoidA::g(){std::coutobj;obj.f();obj.g();return0;}当我尝试编译它时,我收到一个关于有3个而不是两个模板参数的错

c++ - 多个 SFINAE 规则

阅读此question的答案后,我了解到SFINAE可用于根据类是否具有某个成员函数来在两个函数之间进行选择。相当于下面的,只是将if语句中的每个分支拆分成一个重载函数:templatevoidFunc(T&arg){if(HAS_MEMBER_FUNCTION_X(T))arg.X();else//DosomethingelsebecauseTdoesn'thaveX()}成为templatevoidFunc(T&arg,int_to_type);//ThasX()templatevoidFunc(T&arg,int_to_type);//TdoesnothaveX()我想知道是否可

c++ - SFINAE 的问题

为什么这段代码(M类中的fnc值)没有被SFINAE规则解析?我收到一个错误:Error1errorC2039:'type':isnotamemberof'std::tr1::enable_if'当然type不是成员,它没有在这个通用版本的enable_if中定义,但是如果bool为真则启用这个版本的fnc并且如果它为假则不实例化它背后的整个想法不是吗?有人可以给我解释一下吗?#include#includeusingnamespacestd;templatestructNull;templatestructThrow;templateclassPolicy>structIsThrow