草庐IT

function-call-operator

全部标签

c++ - "unresolved overloaded function type"在 std::function 中有静态函数

尝试将重载静态函数传递给std::function时出现“未解析的重载函数类型”错误。我知道类似的问题,例如this和this.然而,即使那里的答案可以将正确函数的地址放入函数指针中,它们也会因std::function而失败。这是我的MWE:#include#include#includestructClassA{staticstd::stringDoCompress(conststd::string&s){returns;}staticstd::stringDoCompress(constchar*c,size_ts){returnstd::string(c,s);}};voidh

c++ - boost spirit : Difference between operators "%=" and "="

我不明白这两个运算符之间的区别。让我们举一个例子,将像"AA,BB,CC,DD"这样的输入解析成字符串vector。namespaceqi=boost::spirit::qi;classmy_grammar:publicqi::grammar{public:my_grammar():base_type(start){usingqi::_1;usingqi::char_;start=*(char_-qi::lit(','));}qi::rulestart;};据我所知,a%=b等同于a=b[_val=_1]。这很清楚。但另一方面,解析器*(char_-qi::lit(','))具有std

c++ - 在调用之前检查 std::function 的有效性?

我正在尝试编写一个简单但灵活的事件系统(主要是作为练习,我知道现有的库具有非常好的事件处理程序),但我遇到了一个小绊脚石。如何检查作为委托(delegate)的std::function(可能通过lambda,可能通过std::bind)是否为有效函数/成员函数的对象是否仍然存在在打电话之前?我试过简单地使用std::function的bool运算符,但没有取得任何成功。理想情况下,我希望A.在委托(delegate)函数内部以外的地方进行检查,并且B.当被检查的std::function不是委托(delegate)时,代码仍然有效。有什么想法吗?编辑:这是我运行的测试的源代码#inc

c++ - std::call_once 是免费的吗?

我想知道std::call_once锁是否空闲。There是使用互斥锁的call_once实现。但是我们为什么要使用互斥体呢?我尝试使用atomic_bool和CAS操作编写简单的实现。代码线程安全吗?#include#include#include#includeusingnamespacestd;usingmy_once_flag=atomic;voidmy_call_once(my_once_flag&flag,std::functionfoo){boolexpected=false;boolres=flag.compare_exchange_strong(expected,tr

c++ - 哪个更好 : returning tuple or passing arguments to function as references?

我创建的代码中有两个函数returnValues和returnValuesVoid。一个返回2个值的元组,另一个接受参数对函数的引用。#include#includestd::tuplereturnValues(constinta,constintb){returnstd::tuple(a,b);}voidreturnValuesVoid(int&a,int&b){a+=100;b+=100;}intmain(){auto[x,y]=returnValues(10,20);std::cout我读到了http://en.cppreference.com/w/cpp/language/st

c++ - 如何使用函数签名的 typedef 作为 std::function 的类型参数?

当用作模板类型参数时,函数的typedef和使用裸函数类型之间肯定有区别。即,考虑#includetypedefstd::functionTF1;typedefvoid(*FooFn)(int);typedefstd::functionTF2;intmain(){TF1tf1;TF2tf2;return0;}我可以创建TF1但不能创建TF2(错误:aggregate'TF2tf2'的类型不完整,无法定义)。(参见ideoneexample。)有没有办法使用函数(签名)的typedef作为模板类型参数;具体来说,作为std::function?的类型参数(没有C++11标记,因为我对bo

c++ - 为什么继承的 protected operator=() 具有公共(public)访问权限

声明为protected的重载运算符=对于继承父类作为public的子类是公开可访问的。#includeclassA{public:A(charc):i(c){}chari;protected:A&operator=(constA&rdm){std::cout编译时没有错误:$g++-Wall-otest_operator~/test_operator.cpp$./test_operatora.i==aaccessingoperator=()a.i==x直接使用A是编译不过的。operator=()以外的任何其他运算符重载都不会编译。使用g++4.4.7和7.3.0以及c++98和c+

c++ - "Socket operation on non-socket"错误由于奇怪的语法

我在调用connect时在我的一些网络代码中遇到错误Socketoperationonnon-socket并花了很多时间试图找出导致的原因它。我终于发现是以下代码行导致了问题:if((sockfd=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol)看到问题了吗?该行应该如下所示:if((sockfd=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol))我不明白的是为什么第一行不正确的行没有产生警告。换句话说,一般形式不应该:if(foo=bar()编译器看起来很奇怪,尤其是使

c++ - 在 switch 语句中使用类类型 : is it better than using typeid operator?

我在下面看到了有关C++标准$6.4.2中switch语句的内容。Switch语句可以带一个条件。Theconditionshallbeofintegraltype,enumerationtype,orofaclasstypeforwhichasingleconversionfunctiontointegralorenumerationtypeexists(12.3).Iftheconditionisofclasstype,theconditionisconvertedbycallingthatconversionfunction,andtheresultoftheconversion

c++ - 语法糖 : automatically creating simple function objects

我要实现一组类模板和两个特殊变量,_1和_2.他们应该使以下内容成为合法代码://Sortascendingstd::sort(a,a+5,_1>_2);//Outputtoastreamstd::for_each(a,a+5,std::cout(std::cout,""),_1+5);我想_1*5也应该产生一个一元函数,以及_1/5等。不允许提升不允许使用lambda现在我有非常对模板和模板元编程的经验很少,所以我什至不知道从哪里开始以及我的类模板的结构应该是什么样子。我特别困惑,因为我不知道在我的类模板中是否必须为所有这些编写实现operator=,operator>>,opera