草庐IT

c++ - 替换失败是依赖非类型模板参数的错误吗?

假设我有这些模板别名:enumclassenabler{};templateusingEnableIf=typenamestd::enable_if::type;templateusingDisableIf=typenamestd::enable_if::type;我可以在GCC中执行以下操作:#includetemplate>={}>voidf(T){std::cout>={}>voidf(T){std::cout它打印:ispolymorphicisnotpolymorphic这符合我的期望。使用clang时代码无法编译。它会产生以下错误消息。test.cpp:11:58:erro

具有 decltype : substitution failure becomes an error? 的 C++ SFINAE

此代码有效://CodeA#include#include#includeusingnamespacestd;templatestructS{template()))>::value>::type>S(Iter){coutv;Ss1(v.begin());//stdout:S(Iter)Ss2(1);//stdout:S(int)}但是下面这段代码不起作用。在下面的代码中,我只想继承std::enable_if,所以类is_iter_of将具有成员typedeftype如果选择的版本std::enable_if具有成员typedeftype。//CodeB#include#includ

c++ - sfinae 使用 decltype 检查静态成员

我写了下面的代码来尝试检测一个类型是否有一个静态成员变量。不幸的是,它总是返回变量不存在。有人能告诉我哪里错了吗?我正在使用g++4.7.1。#include#include#includeusingnamespacestd;templateclasshas_is_baz{template::value>::type...>staticstd::true_typecheck(int);templatestaticstd::false_typecheck(...);public:staticconstexprboolvalue=decltype(check(0))::value;};st

c++ - 为什么 SFINAE 在默认函数参数的右侧不起作用?

我有这个代码:structMy{typedefintfoo;};structMy2{};templatevoidBar(constT&,intz=typenameT::foo()){std::cout我想,如果某个类T内部没有typedeffoo,编译器应该排除第一个重载并选择带省略号的重载。但是我在MSVC、gcc和clang上检查了这段代码,我在这些编译器上遇到了编译错误。为什么SFINAE在这种情况下不起作用? 最佳答案 z的类型不受模板替换的影响,它总是int。这意味着SFINAE没有机会,而是在尝试将T::foo解析为默认

c++ - 如何确定一个类型是否可以仅使用 const 引用调用?

我想写一个C++元函数is_callable定义value成为true,当且仅当类型F具有SomeReturnTypeoperator()(constArg&)形式的函数调用运算符时.例如,在下面的情况下structfoo{voidoperator(constint&){}};我要is_callable成为false和is_callable成为true.这是我到目前为止所拥有的:#include#includetemplatestructis_callable{private:templatestaticchar(&test(...))[2];templatestructhelper{

c++ - 为什么有时需要写 `typename T` 而不是 `T` ?

我正在阅读有关SFINAE的维基百科文章并遇到以下代码示例:structTest{typedefintType;};templatevoidf(typenameT::Type){}//definition#1templatevoidf(T){}//definition#2voidfoo(){f(10);//call#1f(10);//call#2withouterrorthankstoSFINAE}现在我实际上以前写过这样的代码,不知何故凭直觉我知道我需要键入“typenameT”而不仅仅是“T”。但是,很高兴知道其背后的实际逻辑。有人愿意解释一下吗? 最佳

c++ - 使 std 的数据结构默认使用我现有的非静态哈希函数 "hashCode()"

我有一个中等大小的代码库(>200.cpp),它使用函数hashCode()返回哈希值:-classB01{//aclass//.....complexthing....public:size_thashCode(){/*hashalgorithm#H01*/}};classB02{//justanotherunrelatedclass//.....complexthing....public:size_thashCode(){/*#H02*/}//Thisisthesamenameasabove};我已经在不同的地方使用过它,例如在我的自定义数据结构中。它运行良好。现在,我想让std

c++ - 为 SFINAE 测试仪提供默认值零的原因是什么?

我注意到很多boost和libc++/libstdc++在代码中明确地为SFINAE提供了零默认值//libc++http://llvm.org/svn/llvm-project/libcxx/trunk/include/memorynamespace__has_pointer_type_imp{templatestatic__two__test(...);templatestaticchar__test(typename_Up::pointer*=0);}templatestruct__has_pointer_type:publicintegral_constant(0))==1>{

c++ - 如果存在则调用成员函数,回退到自由函数,反之亦然

如果它存在于T上,并且它不调用自由函数,我可以编写一个带有参数T调用成员函数foo的模板函数吗foo(T)代替(如果两者都不存在则无法编译)?类似于:templateintcall_foo(Tt){//ifT::foo()existsreturnt.foo();//elsereturnfoo(t);}相反的情况如何:在成员函数之前优先选择自由函数foo?我无法使用C++11之后引入的任何功能。 最佳答案 这并不难。有许多方法可以检查任意表达式是否有效。您可以将它与C++17中的ifconstexpr或更早的标记分派(dispatch

c++ - 在编译时检测 typedef(模板元编程)

我目前正在做一些模板元编程。就我而言,我可以处理任何“可迭代”类型,即typedeffooconst_iterator以相同方式存在的任何类型。我试图为此使用新的C++11模板元编程,但是我找不到检测某个类型是否丢失的方法。因为我还需要根据其他特征打开/关闭其他模板特化,所以我目前正在使用带有两个参数的模板,第二个是通过std::enable_if生成的。这是我目前正在做的事情:templatestructFoo{};//defaultcaseisinvalidtemplatestructFoo::value>::type>{voiddo_stuff(){...}};templates