jjzjj

c++ - 对于 CLang 中的 enable_if 错误(错误 11723)是否有更好的解决方法?

理想情况下,我们可以使用enable_if做类似的事情:#includenamespacedetail{enumclassenabler_t{DUMMY};}templateusingenable_if_u=typenamestd::enable_if::type;templateusingdisable_if_u=typenamestd::enable_if::type;template::value>...>inta(){return0;}template::value>...>doublea(){return0.0;}intmain(){autox=a();}恕我直言,这是最好的

c++ - 变量 args SFINAE 默认构造函数在 clang 中工作,但在 Visual Studio 2015 中失败

任何人都知道这段代码是否不好,或者VS是否有错误,或者Clang是否允许?我认为我的构造函数不应该接受任何参数并通过enable_if检查-但VS在某处说“不”。VisualStudio2015Update2出现以下错误:source_file.cpp(##):errorC2512:'Foo::Foo':noappropriatedefaultconstructoravailable现场直播:http://rextester.com/VWAI2954VS存在错误:http://rextester.com/PTDSS2853#include#includeusingnamespacest

c++ - 使用省略号的回退函数 : can we force the size of the parameters pack?

考虑以下代码:#include#includestructS{templateautof(A&&...args)->decltype(std::declval().f(std::forward(args)...),void()){std::coutvoidf(...){std::cout(42);//->hasf(int)s.f(42);//->hasnotf(int)//oopss.f();//->hasnotf(int)}如示例所示,对f的第三次调用工作正常,即使参数数量错误,因为对于回退函数来说它根本没有错.当以这种方式涉及省略号时,有没有办法强制参数的数量?我的意思是,我可以在

c++ - C++ 中用于自动将对象转换为 "printable"格式的重载歧义

我正在尝试编写一个接受输入的函数。如果该输入可以直接通过管道传输到流(例如使用std::cout),它会原封不动地返回输入。否则,它会尝试将输入转换为字符串,并返回该字符串。我有以下代码://UsesSFINAEtodeterminewhichoverloadtocall//See:https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error//Basically,make_printabledetectswhetheranobjectcanbedirectlypipedtoastream.//Ifitcan'tb

c++ - 为什么以下 SFINAE 测试无法检测到模板成员函数?

使用GCC编译时,我从以下代码中得到的结果总是错误的。我相信这是一个编译器错误,但有人可能知道得更多。#includetemplateclasshas_apply{typedefcharyes[1];typedefcharno[2];templatestructbinder{};templatestaticyes&test(U*,binder>*=0);templatestaticno&test(...);public:staticconstboolresult=(sizeof(yes)==sizeof(test((T*)(0))));};classA{public:templatev

c++ - 无法在 Visual Studio 10 中编译 SFINAE

#include#include#include#include#include#includestructX{};structY{};__int8f(Xx){return0;}__int16f(...){return0;}templatetypenamestd::enable_if::typecall(Tconst&t){std::couttypenamestd::enable_if::typecall(Tconst&t){std::cout这里好像是很简单的SFINAE的使用,但是编译器报错,就是不能实例化enable_if::type.有什么建议么?显然这段代码在GCC上编译得很

c++ - 确定仿函数的参数和结果类型

我如何测试仿函数是否是一个可调用对象,它引用一个int并返回一个bool?templatevoidfoo(functorf){static_assert('functor==bool(int&)',"errormessage");intx=-1;if(f(x))std::cout 最佳答案 在我看来,您并不是真的想检查仿函数的签名,而是首先要限制用户可以传入的内容:如果您有权访问std::function,您可以这样做:voidfoo(conststd::function&f) 关于c+

c++ - 用于测试 func(args) 是否格式正确且具有必需的返回类型的特征

有许多类似的问题/答案,但我无法完全将这些答案放在一起来满足我的目的。我想要一个特质templatestructreturns_a{staticconstboolvalue;};这样returns_a::value如果F(Args)格式正确并返回T,则为真。经过更多研究后,我的工作方式如下://valueistrueifFunc(Args...)iswellformedtemplateclassis_callable{templatestaticdecltype(std::declval()(std::declval()...),void(),0)test(int);templates

c++ - 为什么 SFINAE 需要 'Enable' 类模板参数?

(本题与C++11/C++14无关:示例使用C++03编译)enable_bool有成员(member)::type仅当T是booltemplatestructenable_bool{};templatestructenable_bool{typedefbooltype;};在下一个片段中,部分特化是正确的(参见gcc.godbolt.org)templatestructFoo{staticintbar(){return0;}};templatestructFoo::type>{staticintbar(){return1;}};intmain(){returnFoo::bar();}

c++ - 强制转换尾随返回类型会导致 SFINAE 失败

出于学习目的,我重新实现了boost::hana::is_valid。用例是:structPerson{std::stringname;};intmain(){autohas_name=is_valid([](auto&&t)->decltype((void)t.name){});Personjon{"snow"};static_assert(has_name(jon),"");static_assert(!has_name(1),"");}实现:namespacedetail{templatestructis_valid_impl{template>constexprbooloper