草庐IT

enable-mirrors

全部标签

c++ - enable_if 和转换运算符?

有没有机会将enable_if与类型转换运算符一起使用?看起来很棘手,因为返回类型和参数列表都是隐式的。 最佳答案 根据我所做的小研究(并忽略Johannes的c++0x评论),我的回答是这取决于您想要什么enable_if为了。如果要转换操作为T存在与否来自类型T那么似乎答案是否定的,在C++03中没有办法(正如Ugo所说)。但如果您需要enable_if根据T的类型更改运算符(operator)的行为那么是的,有一种解决方法是调用启用的辅助函数(称为to,正如Matthieu建议的那样)。#include#include#inc

c++ - std::enable_if 是如何工作的?

我刚刚问了这个问题:std::numeric_limitsasaCondition我了解std::enable_if将定义方法的返回类型的用法,有条件地导致方法编译失败。templatetypenamestd::enable_if::is_integer,void>::typefoo(constT&bar){isInt(bar);}我不明白的是第二个参数以及当std::enable_if声明为模板语句的一部分时看似毫无意义的赋值,如Rapptzanswer.template::value,int>::type=0>voidfoo(constT&bar){isInt();}

c++ - 使用不同的 enable_if 条件选择成员函数

我正在尝试根据类模板参数确定调用哪个版本的成员函数。我试过这个:#include#includetemplatestructPoint{voidMyFunction(typenamestd::enable_if::value,T>::type*=0){std::cout::value,float>::type*=0){std::coutintPoint;intPoint.MyFunction();PointfloatPoint;floatPoint.MyFunction();}我认为是说“如果T是int,则使用第一个MyFunction,如果T不是int,则使用第二个MyFunctio

c++ - 如何将 std::enable_if 与自推断返回类型一起使用?

C++14将具有可以根据返回值推断返回类型的函数。autofunction(){return"helloworld";}我可以将此行为应用于使用enable_if的函数吗?对于SFINAE的返回类型习语?例如,让我们考虑以下两个函数:#include#include//Thisfunctionischosenwhenanintegraltypeispassedintemplateautofunction(Tt)->typenamestd::enable_if::value>::type{std::coutautofunction(Tt)->typenamestd::enable_if:

带有 enable_if : different behaviour with g++ and clang 的 C++ 模板重载

在解决基类的模板化成员函数的重载时,我观察到g++(5.2.1-23)和clang(3.8.0)之间的不同行为,-std=c++14.#include#includestructBase{templateautoa(Tt)->void{std::coutstructDerived:publicBase{usingBase::a;templateautoa(Tt)->std::enable_if_t{std::coutd;d.a(1);//failswithg++,prints"true"withclangDerivedd2;d2.a(1);//failswithclang++,prin

c++ - std::enable_shared_from_this;公共(public)与私有(private)

我使用shared_ptr和enable_shared_from_this玩了一会儿,但遇到了一些我不太了解的东西。在我的第一次尝试中,我构建了这样的东西:classshared_test:std::enable_shared_from_this{public:voidprint(boolrecursive){if(recursive){shared_from_this()->print(false);}std::cout请注意,这个类正在私下扩展std::enable_shared_from_this。这显然有很大的不同,因为执行这样的事情:intmain(){autot(std::

c++ - 为什么 SFINAE (enable_if) 不适用于类模板的成员函数?

#includestructA{};structB{};templatestructFoo{typenamestd::enable_if::value>::typebar(){}typenamestd::enable_if::value>::typebar(){}};错误信息:14:5:error:'typenamestd::enable_if::value>::typeFoo::bar()'cannotbeoverloaded10:5:error:with'typenamestd::enable_if::value>::typeFoo::bar()'来源cpp.sh.我以为都是typ

c++ - std::conditional vs std::enable_if

我有一个散列函数,它可以接受任何对象类型并对其进行散列,它使用std::hash内部。因为std::hash不支持枚举类型我创建了函数的重载,1用于枚举使用std::underlying_type其他类型为1:template::value>::type*=nullptr>staticstd::size_tHash(Tconst&t){returnstd::hash::type>()(t);}template::value>::type*=nullptr>staticstd::size_tHash(Tconst&t){returnstd::hash()(t);}这工作正常。然后我尝试使

c++ - 使用不依赖于方法模板参数的 enable_if

我正在尝试使用std::enable_if和SFINAE以完全基于类的模板参数切换类模板方法的实现。示例:#includetemplateclassFoo{templatetypenamestd::enable_if::value,void>::typebar(InnerTparam){};templatetypenamestd::enable_if::value,void>::typebar(InnerTparam){};};intmain(){Foof;}这里,bar()应该根据T1是否有不同的行为和T2是否相同类型。但是,此代码无法编译。GCC和clang都没有告诉我任何有用的信

c++ - 完美转发 setter 的正确 `enable_if` 约束是什么?

HerbSutter的回归基础!CppCon上的现代C++基础介绍讨论了传递参数的不同选项,并比较了它们的性能与编写/教学的难易程度。“高级”选项(在所有测试的情况下提供最佳性能,但对于大多数开发人员来说太难编写)是完美的转发,给出的示例(PDF,pg.28):classemployee{std::stringname_;public:template,std::string>::value>>voidset_name(String&&name)noexcept(std::is_nothrow_assignable::value){name_=std::forward(name);}}