草庐IT

c++ - 如何通过 SFINAE 测试类中是否存在内部类?

我正在尝试为具有特定名称的内部类的类设置不同的模板特化。我从here中得到了线索并尝试了以下操作:#includetemplatestructHasXYZ{staticconstboolvalue=false;};templatestructHasXYZ{staticconstboolvalue=true;};structFoo{classXYZ{};};structFooWithTypedef{typedefvoidXYZ;};intmain(){//Thefollowinglineprints1,asexpectedstd::cout::value::value如您所见,如果我在F

c++ - 使用 SFINAE 检测成员函数

这个问题在这里已经有了答案:Templatedcheckfortheexistenceofaclassmemberfunction?(33个答案)关闭9年前。在C++11中,要确定一个类是否具有成员函数size,您可以定义以下测试助手:templatestructhas_size_fn{typedefchar(&yes)[1];typedefchar(&no)[2];templatestaticyescheck(decltype(&C::size));templatestaticnocheck(...);staticboolconstvalue=sizeof(check(0))==si

c++ - SFINAE 只有在可能的情况下才有类(class)成员

我做了一个类型安全的ID类,但现在我想支持operator++如果底层类型也有它。来自this和thisanswears我已经想出了2个替代方案,但是当用AId实例化时它们都失败了:templatestructAId{typedefAIdtype;typedefThandled_type;typedefTIDvalue_type;private:value_typeid;templatestructIsIncrementable{templateusingrm_ref=typenamestd::remove_reference::type;typedefchar(&yes)[1];ty

c++ - SFINAE 和 noexcept 说明符

是否在noexcept中表达说明符的括号在函数模板的重载解析过程中参与SFINAE?我想为聚合做一个包装器并想要std::is_constructible谓词为其正常工作:templatestructembrace:type{templateembrace(arguments&&..._arguments)noexcept(noexcept(type{std::forward(_arguments)...})):type{std::forward(_arguments)...}//braces{;}};intmain(){structS{inti;doublej;};//aggrega

c++ - 在 SFINAE 上下文中作为模板模板参数传递的引用类型的别名模板

我在使用G++6.1.0(-std=c++14开关)时遇到了以下问题,我不明白为什么编译器会拒绝该代码。我有一个辅助结构is_well_formed当将另一个提供的类型替换为它时,它检查提供的模板模板参数是否格式正确:templatetypenameR,typenameT,typename=void>structis_well_formed:std::false_type{};templatetypenameR,typenameT>structis_well_formed>>:std::true_type{};我想检查一个类型是否可引用。所以我的想法是编写以下内容://(#1)temp

c++ - 有没有办法编写 "for-eachability"类型的 SFINAE 测试?

我使用SFINAE表达式来测试类型是否支持operatornamespacedetails{templatestructsfinae_true:std::true_type{};templatesfinae_true()())>test_for_ostream(int);templatestd::false_typetest_for_ostream(long);}templatestructsupports_ostream:decltype(details::test_for_ostream(0)){};我想测试的是这是否是一个类型T可以像这样迭代for(auto&&v:vs){}//

c++ - 这被认为是 SFINAE 吗?

大约一周前我问了一个问题,询问我如何才能简单地实例化一个类模板,前提是它采用的类型具有特定的成员函数。在我的回答中,我得到了一个复杂的解决方案。但后来我试着自己做。我只是想知道这是否足以找出给定类型T有一个名为f的void函数采用0个参数。#include#includetemplatestructhas_f:std::false_type{};templatestructhas_f().f(),void())>:std::true_type{};template::value>::type>structA{};structB{voidf();};structC{};templatec

c++ - 为什么 sfinae 过载没有解决

这个版本工作正常:templatestructFoo{templatetypenamestd::enable_if::value>::typebar(){std::couttypenamestd::enable_if::value>::typebar(){std::cout此版本失败:templatestructFoo2{template::value>::type>Vbar(){std::cout::value>::type>Vbar(){std::cout与:error:'templatetemplateVFoo2::bar()'cannotbeoverloadedwith'tem

c++ - 带有 decltype : bug in clang or gcc? 的 sfinae

Clang-3.2可以编译并且代码按预期运行:structhave_f{intf(inti){return10;}};structempty{};templatestructouter{Tt;//ifThavef(),defineouter_f()templateintouter_f(inti){returnt.f(i);}};intmain(){outero1;outero2;//tosilenceunusedvarwarningreturno1.outer_f(10)+sizeof(o2);}任何版本的GCC拒绝:t.cc:13:6:error:‘structempty’hasno

c++ - 静态断言和 SFINAE

考虑一下:templatestructhash{static_assert(false,"Notimplemented.");};structunhashable{};templateautotest(constT&t)->decltype((*(hashconst*)nullptr)(t),int);voidtest(...);intmain(){std::cout())),void>::value;}除了明显缺少header之外,这应该编译吗?换句话说,我问的是在推导重载函数模板的返回值时是否要求在尾随decltype内触发静态断言失败以停止编译,或者是否只需丢弃重载。在gcc4.