草庐IT

c++ - 函数指针 : is the simple canonical use bad from a performance point of view? 如果是的话,c++11-ish 的替代方案是什么?

我在我的c++代码中经常使用函数指针,总是以符合这个简单规范示例的方式使用(例如,函数具有相同的I/O,但所需的操作只是在运行时已知):#includeusingnamespacestd;intadd(intfirst,intsecond){returnfirst+second;}intsubtract(intfirst,intsecond){returnfirst-second;}intoperation(intfirst,intsecond,int(*functocall)(int,int)){return(*functocall)(first,second);}intmain()

c++ - 为什么 "is_modulo"对于 double 和 float 是假的?

我写了一些代码来检查一个类型是否有模表示:#include#includeusingnamespacestd;intmain(){cout::is_modulo::is_modulo输出:Whetherfloatobjectshaveamodulorepresentation:0Whetherdoubleobjectshaveamodulorepresentation:0但是我们可以使用fmod()(来自)找到float的模数或double.那么,为什么is_modulo如果可以找到float或double的模数,则为false? 最佳答案

C++ 标准 : return by copy to initialize a reference without RVO: is there any copy?

让我们考虑下一个示例:structbig_type{};//Returnbycopyautofactory(){returnbig_type{};}voidany_scope_or_function(){big_type&&lifetime_extended=factory();}假设RVO被禁止或根本不以任何方式存在,big_type()是否会或可以被复制?还是将引用直接绑定(bind)到return语句中构造的临时对象?我想确保big_type析构函数仅在any_scope_or_function结束时被调用一次。我使用C++14,以防某些行为在标准版本之间发生变化。

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++ - C++ 模板如何专用于所有 32 位 POD 类型?

我开发了一个简单的模板函数来交换单个字段的字节顺序:templateinlinevoidSwapEndian(T&ptr){char*bytes=reinterpret_cast(&ptr);inta=sizeof(T)/2;while(a--){chartmp=bytes[a];intb=sizeof(T)-1-a;bytes[a]=bytes[b];bytes[b]=tmp;}}我经常在T=int或float的地方使用它。这两种类型在目标平台上均由4个字节表示,并且可以由模板的相同特化处理。因为这个函数有时负责处理大缓冲区的原始数据,所以我创建了一个优化的特化:templatein

具有预增量 : With or without parentheses is the same? 的 C++ 箭头运算符

类(class)问题:Watchtheparenthesesaroundtheargumentofthe++operator.Aretheyreallyneeded?Whatwillhappenwhenyouremovethem?最初只有一个cout表达式。我添加了另一个以查看差异,如下所示:#includeusingnamespacestd;classClass{public:Class(void){coutvalue=0;coutvalue)value)我的想法是在没有括号的情况下再次测试它,看看有什么不同:...coutvaluevalue两种情况下的结果是一样的。因此我得出结论

C++ Linux : error: ‘move’ is not a member of ‘std’ how to get around it?

所以在我的VS2010上我可以编译如下代码:boost::shared_ptrinternal_thread;boost::packaged_taskinternal_task_w(boost::bind(&thread_pool::internal_run,this,internal_thread));internal_thread=boost::shared_ptr(newboost::thread(std::move(internal_task_w)));前两行在boost1.47.0和linux上没问题...但是在std::move上它给出了error:‘move’isnota

c++ - C++11 中的默认构造函数、POD 的初始化和隐式类型转换

我刚看了Chandler在GoingNative2012上关于Clang的演讲。他展示了以下代码:#includestructS{intn;};structX{X(int){};};voidf(void*){std::cerrChandler指出,这为c++11调用了f(void*),为c++03调用了f(X)。他还指出,原因是S().n默认初始化为0,使其成为nullptr常量。首先,我假设成员变量n的零初始化依赖于编译器实现并且不受标准保证(或者这是否随c++11发生了变化)?Chandler暗示这是由于支持常量表达式,但我仍然不能完全理解他的推理。其次,为什么f(X)会被C++0

C++11:可变同质非 POD 模板函数参数?

在C++11中,您将如何编写一个采用可变数量的同类非POD函数参数的模板函数?例如,假设我们想为定义小于“operator//pseduo-code...templateTmin(Tx1,Tx2,...,Txn){Tlowest=x1;for(Tx:{x2,...,xn})if(x上面是非法的C++11,你怎么写才合法? 最佳答案 均匀?只需使用std::initializer_list。templateTmin_impl(std::initializer_listvalues){return*std::min_element(va