草庐IT

some_friend_function

全部标签

c++ - 如何在模板类中声明 friend 用户定义的文字运算符?

目前还不清楚为什么下面的代码不能用GCCg++4.7编译,说明如下:$g++-std=c++11-fPICtest.cpptest.cpp:11:45:error:‘Boperator""_b(constchar*,size_t)’hasinvalidargumentlist如果类C被声明为非模板,那么它可以正常编译。#includestructB{};Boperator+(B,B){returnB();}Boperator""_b(constchar*,size_t){returnB();}templateclassC{friendBoperator+(B,B);friendBope

c++ - friend 功能在这里得到继承吗?

Derived类中的方法fun()是私有(private)的。当我们通过运行时多态调用函数ptr->fun()时,它正在执行。但这违反了派生类的封装属性。#includeusingnamespacestd;classDerived;classBase{private:virtualvoidfun(){coutfun();return0;}谁能解释一下发生了什么? 最佳答案 首先,你的Derived::fun()也是virtual,因为如果派生类中的函数与派生类中的虚函数具有相同的声明基类,派生类中的函数自动获得virtual,即使没

c++ - 为任意类型删除实现小缓冲区优化的简单方法(如在 std::function 中)。

我倾向于使用类型删除技术。它通常看起来像这样:classYetAnotherTypeErasure{public://interfaceredirectedtopImplprivate://Adaptingfunctiontemplate...friendYetAnotherTypeErasuremake_YetAnotherTypeErasure(...);classInterface{...};templateclassConcretefinal:publicInterface{//redirectingInterfacetoAdaptee};std::unique_ptrpImp

c++ - 与 friend 一起上课而不是前向声明,: which compiler is correct

这个问题在这里已经有了答案:Friendmethod"notdeclaredinthisscope"inC++(1个回答)Error:'FriendMemberFunctionName'wasnotdeclaredinthisscope(3个答案)关闭3年前。我有这个简单的C++程序:#includestructobj{friendintf(int);voidm(intx){std::cout如果我使用GNUC++编译器g++进行编译,我会得到错误prog.cpp:7:55:error:'f'wasnotdeclaredinthisscope但是,如果我使用cl(和/W4)编译它,它会

c++ - ADL 和 friend 注入(inject)

考虑这段代码:templatestructX{friendvoidf(X*){}};intmain(){f((X*)0);//Error?}编译器似乎非常不同意。(MSVC08/10说不是,GCC根据“C++模板-完整指南”:...itisassumedthatacallinvolvingalookupforfriendsinassociatedclassesactuallycausestheclasstobeinstantiated...AlthoughthiswasclearlyintendedbythosewhowrotetheC++standard,itisnotclearly

c++ - 如何在不传递大量参数的情况下处理类的 "many small functions"的想法?

随着时间的推移,我开始欣赏许多小函数的思维方式,我真的很喜欢它,但我很难摆脱羞怯将它应用到类中,尤其是那些有很多小函数的类非公共(public)成员变量。每个额外的辅助函数都会使界面变得困惑,因为代码通常是特定于类的,我不能只使用一些通用代码。(据我所知,无论如何,还是一个初学者,不知道那里的每个图书馆等)所以在极端情况下,我通常会创建一个辅助类,它成为需要操作的类的友元,因此它可以访问所有非公共(public)内容。另一种方法是需要参数的自由函数,但即使过早优化是邪恶的,而且我还没有真正分析或反汇编它......我仍然害怕有时仅仅想到传递我需要的所有东西,即使只是作为引用,即使每个参

c++ - 在编译时捕获 std::function 分配

我只想允许在我的代码库中使用std::function,前提是它不进行任何分配。为此,我可以编写类似下面的函数,并且只用它来创建我的函数实例:templatestd::functionmakeFunction(Functorf){returnstd::function(std::allocator_arg,DummyAllocator(),f);}如果DummyAllocator在运行时被使用,它将在何处断言或抛出。理想情况下,我想在编译时捕获分配用例。即templatestd::functionmakeFunction(Functorf){static_assert(sizeneed

c++ - 当 std::stringstream 使用 ("some content"构造时,tellg() 的预期行为是什么,std::ios::in|std::ios::ate)?

我有以下令我惊讶的代码(使用libstdc++4.8)...#include#include#includeusingnamespacestd;intmain(){std::strings("somecontent");std::stringstreamss(s,std::ios::in|std::ios::ate);std::istream&file=ss;//ss.clear();Makesnodifference...std::cout...具有以下输出。tellg()pos:0此行为与使用std::ifstream(std::ios::ate)时不同。此行为是否正确/符合预期?

c++ - SFINAE : Detecting if a function is called by a compile time known value

我喜欢在我的一个ctors以编译时已知值被调用时做一些检查。有办法检测吗?所以当有人调用它时:Aa(10);因为10是编译时已知常量,所以我喜欢调用一个特殊的构造函数,如下所示:template>A(intValue){}知道如何解决这个问题吗?谢谢! 最佳答案 积分常量可以解决您的问题:structA{template*=nullptr>A(std::integral_constant){}};然后,你可以像这样使用它:Aa{std:integral_constant{}};为了便于使用,您还可以使用类似于boost::hana的

c++ - std::function 的移动赋值不是 noexcept,但 std::function::swap 是。是什么赋予了?

std::function在func.wrap.func中的概要告诉我们function&operator=(function&&);移动赋值运算符不是noexcept,禁止将其用于标准容器中的仅移动类型。但是!它还告诉我们voidswap(function&)noexcept;同样,默认的构造函数是function()noexcept;因此我们可以使用默认构造函数后跟交换来实现移动构造函数。因为我们可以用swap实现移动赋值运算符(swap有更强的后置条件):如何在std::function中实现noexcept交换?为什么std::function的移动赋值运算符不是noexcep