草庐IT

friend-function

全部标签

c++ - 与模板特化成为 friend 时可能出现 gcc 错误

在回答关于SO的另一个问题时,我遇到了一个有点可疑的gcc编译器错误。有问题的片段是templateclassA;templatevoidoperator*(A,A);templateclassA{friendvoid::operator*(A,A);...最后一行给出了著名的警告frienddeclaration'voidoperator*(A,A)'declaresanon-templatefunction稍后会导致硬错误。完整代码可见here.现在,问题是我认为这种行为不合适。[temp.friend]/1中的标准说:Forafriendfunctiondeclarationth

c++ - 来自不同命名空间的模板模板参数可以成为 friend 吗?

如果这个问题的标题没有帮助,我深表歉意;如果不给出以下示例,我不知道如何简洁地提出这个问题:templateclassArg>classC{typedefCtype;friendclassArg;public:C(){a_.set(this);}private:inti_;Arga_;};templateclassArg1{public:voidset(Type*t){t_=t;t_->i_=1;}private:Type*t_;};namespaceNS{templateclassArg2{public:voidset(Type*t){t_=t;t_->i_=2;}private:T

c++ - 为什么宏 __STL_FUNCTION_TMPL_PARTIAL_ORDER 应该将模板函数包含在 std_pair.h 中

今天在STL_pair.h中看到如下代码:#ifdef__STL_FUNCTION_TMPL_PARTIAL_ORDERtemplateinlinebooloperator!=(constpair&__x,constpair&__y){return!(__x==__y);}templateinlinebooloperator>(constpair&__x,constpair&__y){return__y我不认为模板函数与偏特化有任何关联的功能模板。我错了吗? 最佳答案 编译器如何处理函数调用在C++中调用函数模板经历了名称查找(标准

C++ 设计 : Overloading/Overriding many many functions, 的清理方式?

我在这里尝试实现的案例是一个基类,它有一个函数(我们称之为modify_command),它实际上可以接受许多不同的类型,因此派生类可以实现它们认为合适的modify_command函数。现在我在基类中有一些类似的东西:classBase{templatevoidmodify_command(Commandcmd){std::cout(cmd);//Callsthetemplatedfunction}virtualvoidmodify_command(SpecificCommandBcmd){modify_command(cmd);//Callsthetemplatedfunction

C++0x,带友元运算符的用户定义文字 ""()

将operator""(...)定义为友元函数是否可能和/或有用?classPuzzle{friendPuzzleoperator""_puzzle(constchar*,size_t);...};voidsolve(Puzzle);intmain(){solve("oxo,xox"_puzzle);};我特别考虑“有用”,因为operator""只能在命名空间中定义的规则——尤其是因为标识符以_在全局命名空间中保留。这个friend在这里违反了这个规则吗?所以,这种不完全封装没有任何好处,对吧? 最佳答案 标准在唯一提到对用户定义

c++ - 单步执行 std::function 调用时如何跳过 std 命名空间中的方法调用? (使用 GDB。)

如果我需要通过std::function调用,使用调试器单步执行函数对象可能会非常困惑。是否可以通过某种方式跳过这些帧? 最佳答案 gdb7.4为此添加了skip系列命令。(gdb)aproposskipinfoskip--Displaythestatusofskipssetstep-mode--Setmodeofthestepoperationshowstep-mode--Showmodeofthestepoperationskip--Ignoreafunctionwhilesteppingskipdelete--Deletesk

c++ - 哪个工具可以显示C++中Method或function的最大语句?

关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。关闭7年前。Improvethisquestion我们的项目真的很大。一个模块的源代码大小约为620KLOC。所以我想检查目录/模块中哪个功能最大?有什么工具可以支持吗?SourceMonitor只有“每个方法的平均语句数”,而不是每个方法的最大语句数。中国交建也不支持。例子。该函数长度为1。unsignedshortget(){return1;}谢谢。

c++ - parallel_for (Inter TBB) 是否存在类似于我们在 std::function 上看到的开销?

在此链接中std::functionvstemplate关于std::function的开销有一个很好的讨论。基本上,要避免传递给std::function构造函数的仿函数的堆分配造成10倍的开销,您必须使用std::ref或std::cref。取自@CassioNeri答案的示例显示了如何通过引用将lambda传递给std::function。floatfoo(std::functionf){return-1.0f*f(3.3f)+666.0f;}foo(std::cref([a,b,c](floatarg){returnarg*0.5f;}));现在,IntelThreadBuil

C++ lambda : Access static method in lambda leads to error 'this was not captured for this lambda function'

考虑以下代码://thisiswhatIwanttocall;Icannotmodifyitssignaturevoidsome_library_method(void(*fp)(void));classSingleton{public:staticSingleton*instance();voidfoo();voidbar();private:Singleton();};voidSingleton::foo(){//thisleadstoanerror('this'wasnotcapturedforthislambdafunction)void(*func_pointer)(void

c++ - g++ 错误 : specialization after instantiation (template class as friend)

考虑以下C++代码:templateclassSingleton{};classConcreteSingleton:publicSingleton{templatefriendclassSingleton;};intmain(){}Singleton应该是ConcreteSingleton的friend:它适用于Microsoft的可视化C++编译器。但是,我不能用g++4.8.4编译它。错误是:error:specializationof‘Singleton’afterinstantiationtemplatefriendclassSingleton;有什么办法可以解决吗?