草庐IT

c++ - SFINAE 条件和构造函数参数类型

我遇到了以下允许围绕T构造包装器对象的技术,但是来自U类型的对象,如果T可以从U构造:templatestructS{template::value>>explicitS(U&&arg):value(arg){}...};IIUC,is_constructible测试中使用的类型U可以不同于arg的cv限定类型。尽管表达式value(arg)有效,但SFINAE测试是否有可能失败? 最佳答案 IsitpossiblethattheSFINAEtestcouldfailalthoughtheexpression value(arg) 

c++ - 模板默认类型与默认值

此问题遵循previousone.案例一:默认类型以下程序无法编译并报告errorC2995:'Tfoo(void)':functiontemplatehasalreadybeendefined:#include#includetemplate::value>>Tfoo(){std::cout::value>>Tfoo(){std::cout();foo();}每个模板都由两个foo实例交替使用和忽略(SFINAE)。所以我假设编译器在某个时候看到:templateTfoo(){std::coutTfoo(){std::cout两个定义是一样的,错误有点可以理解。也许不太容易理解的是为

c++ - 成员函数检测的递归type_traits

我正在尝试递归地应用type_traithas_fun以便C仅在T时启用其fun成员函数>有一个。有没有办法让C::fun被有条件地检测到?templatestructhas_fun{templateclasschecker;templatestaticstd::true_typetest(checker*);templatestaticstd::false_typetest(...);staticconstboolvalue=std::is_same(nullptr))>::value;};structA{voidfun(){std::coutstructC{voidfun(){st

c++ - template<class = enable_if_t<...>> 做什么?

我一直在阅读STL文件,以学习格式化代码的更好方法,并学习提高效率的技巧。我一直在阅读线程文件,但我无法弄清楚某些代码的作用。template,thread>::value>>explicitthread(_Fn&&_Fx,_Args&&..._Ax){//constructwith_Fx(_Ax...)...}std::enable_if_t是templateusingenable_if_t=typenameenable_if::type;templatestructenable_if{//typeis_Tyfor_Testusingtype=_Ty;};该代码在thread和str

c++ - 使用 C++,使用 SFINAE 测试静态成员是否存在,返回错误值

我正在学习模板元编程。当尝试使用以下代码测试静态成员时,第二个SFINAE总是返回错误值:#include#include//ts_astructts_a{staticinta;};//ts_bstructts_b{staticintb;};//has_atemplate>structhas_a:std::false_type{};templatestructhas_a>:std::true_type{};//has_btemplate>structhas_b:std::false_type{};templatestructhas_b>:std::true_type{};intmain

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++ - std::enable_if 不能用于禁用此声明

我有一段相当复杂的代码,我将其简化为这个复制器:#include#includetemplatestructouter{templatestructinner{templatestructproblem;usingTA=std::tuple;usingTB=std::tuple;templatestructproblem::value::value>::type>{staticconstexprautoval(){return1;}//actuallyacomplexfunction};templatestructproblem::value>=std::tuple_size::val

c++ - 使作用域枚举与基础类型相当

我试图在我的程序中使作用域枚举与底层类型相当,但以下代码不起作用。是因为我使用的编译器(VC11)对C++11标准的支持很差,还是因为代码违反了C++11标准的一些规则?在后一种情况下,究竟违反了哪些规则(欢迎引用特定的标准条款)?#includeenumclassTest:shortint{A,B,C};templatebooloperator!=(Ee,typenamestd::underlying_type::typen){returnstatic_cast::type>(e)!=n;}templatebooloperator!=(typenamestd::underlying_

c++ - SFINAE:当重载移动到其他 namespace 时,检查函数是否存在中断

我想使用SFINAE检查特定命名空间中是否存在函数。我找到了SFINAEtotestafreefunctionfromanothernamespace哪个完成了工作,但有些事情我不明白。目前我有这个工作代码,直接来自链接的问题://switchto0totesttheothercase#defineENABLE_FOO_BAR1namespacefoo{#ifENABLE_FOO_BARintbar();#endif}namespacedetail_overload{templatevoidbar(Args&&...);}namespacedetail{usingnamespacede

c++ - VS2013中的SFINAE

我有一个函数模板forwardMaybeNull。我希望它可以在第一个参数不是nullptr时转发它,如果第一个参数是nullptr则只返回第二个参数。template::value>...>U&&forwardMaybeNull(std::nullptr_t,U&&rvalue_default){returnstd::move(rvalue_default);}template::value>...>T&&forwardMaybeNull(std::remove_reference_t&arg,U&&){returnstatic_cast(arg);}template::value>