草庐IT

function-call-operator

全部标签

c++ - 无趣的模拟函数调用 bla() && 预期 : to be called at least once bla()?

我用模拟类编写了一个小测试。当我运行它时,首先我得到一个警告,说调用了一个无趣的模拟函数,然后测试失败,因为没有满足预期,即至少调用了一次模拟函数。有趣的是,当我看到上面的警告消息时,该函数被调用了。你对这件事有什么想法吗?谢谢!编辑:这是我的代码结构:classBla{public:Bla();virtual~Bla();virtualfloatmyFunction();}classMockBla:publicBla{MockBla();~MockBla();MOCKMETHOD0(myFunction,float());}classCallerClass{public:Caller

c++ - 错误 : expected primary-expression before ‘>’ : templated function that try to uses a template method of the class for which is templated

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。在使用模板和仿函数(未出现在这个问题中)时,我最终遇到了以下简化的问题。以下代码(也可用here)classA{public:templateboolisGood(intin)const{constTf;returninbooltryEvaluator(T&evaluator,intvalue){returnevaluator.isGood(value);}intmain(intargc,constchar*argv[]

c++ - 为什么在取消引用的函数指针上使用时 std::is_function 的计算结果为 false?

我正在尝试使用std::is_function来确定变量是否为函数指针。运行以下代码时#include#includeusingnamespacestd;intmain(){typedefint(*functionpointer)();functionpointerpmain=main;cout::value::value::value::value输出是PFivE0PFivE0FivE1FivE0任何有见识的人都可以解释为什么std::is_function的最后一个表达式的计算结果为false吗?(代码在g++4.7、g++4.8和clang++3.2下测试)

c++ - 为什么 std::function 不能接受推导类型作为其模板参数?

#includeusingnamespacestd;templatevoidf1(CharType*str,functionfn_filter){}templatevoidf2(CharType*str,functionfn_filter){}voidf3(char*str,charc){autofn_filter=[=](chare)->bool{returne==c;};f1(str,fn_filter);//errorC2784f2(str,fn_filter);//OK}intmain(){f3("ok",'k');}//errorC2784:'voidf1(CharType*

c++ - 为什么 C++ STL 容器使用 "less than"operator< 而不是 "equal equal"operator== 作为比较器?

在std::map的自定义类中实现比较运算符时,我遇到了这个问题,但看不到任何被问到的地方。除了上述问题,也有兴趣简要了解,如何operator适用于std::map.问题来源:structAddress{longm_IPv4Address;boolisTCP;booloperator 最佳答案 std::map需要能够排序。默认情况下使用std::less,对于非指针使用1。使用您对用户的要求最少的规则,它从综合“等价”当它需要它时(!(a表示a和b是等价的,即两者都不小于另一个)。这使得编写用作map的关键组件的类变得更加容易,

c++ - 为什么 std::unary_function 不包含虚拟析构函数

我遇到了类模板std::unary_function和std::binary_function。templatestructunary_function{typedefArgargument_type;typedefResultresult_type;};templatestructbinary_function{typedefArg1first_argument_type;typedefArg2second_argument_type;typedefResultresult_type;};这两个都可以用作特定用途的基类。但是其中仍然没有虚拟析构函数。我猜的原因之一是这些并不意味着要进

c++ - operator bool() 转换为 std::string 并与 operator std::string() 冲突

在类中声明operatorstd::string时,operatorbool()怎么会导致错误,而且它本身还充当到string的隐式转换?#include#includeusingnamespacestd;classTest{public:operatorstd::string(){cout 最佳答案 您面临的问题(除了operatorstd::string()返回bool之外)是隐式转换在您需要和不需要时触发。当编译器看到s=t时,它会识别以下潜在的std::operator=匹配项://usingstd::stringforco

c++ - 将 operator== 重载为带有模板参数的自由函数的语法是什么?

我有一组多态类,例如:classApple{};classRed:publicApple{};classGreen:publicApple{};以及比较它们的自由函数:booloperator==(constApple&,constApple&);booloperator我正在设计一个可复制的包装器类,它将允许我使用类Red和Green作为STL映射中的键,同时保留它们的多态行为。templateclassCopy{public:Copy(constCat&inCat):type(inCat.clone()){}~Copy(){deletetype;}Cat*operator->(){

c++ - 确定 std::function 的返回类型

我正在编写一个模板函数,它接收一个std::function对象(通过使用适当的参数调用std::bind生成)。在这个函数中,我想确定这个函数对象的返回类型。有可能吗?事实上,我希望模板函数返回相同的类型。您能想出一种优雅的、基于标准的方法来实现这一目标吗?类似于:templateT::return_typefunctionObjWrapper(TfunctionObject){//...returnfunctionObject();}谢谢 最佳答案 您可以使用decltype和尾随返回类型来完成:templateautofunc

c++ - 将双变量 std::function 转换为单变量

我有一个获取两个值x和y并返回结果的函数:std::functionmult=[](doublex,doubley){returnx*y;};现在我想得到一个常量y的单变量函数。我写了下面的代码,但它不起作用。std::function(std::function,double)>funcYgivenX=[](std::functionfunc2d,doubleinX){return[&func2d,&inX](doubleinY){returnfunc2d(inX,inY);};};我在这里做错了什么?最好(最有效)的方法是什么? 最佳答案