草庐IT

c++ - 模板友元函数的前向声明

考虑以下运行良好的代码片段:classA{private:intd;public:A(intn){d=n;}friendintfoo(Aa);};intfoo(Aa){returna.d;}但是,当我尝试为该类使用模板时,我需要转发声明友元函数才能运行,如下所示:templateclassB;templateTfoof(Ba);templateclassB{private:Td;public:B(Tn){d=n;}friendTfoof(Ba);};templateTfoof(Ba){returna.d;}为什么前向声明在第二个例子中是必要的,而在第一个例子中却不需要?另外,为什么我必

c++ - g++ 和 clang++ 与在模板类中定义的友元模板函数的不同行为

另一个类型的问题“g++和clang++之间谁是正确的?”适用于C++标准专家。下面的代码templatestructfoo{templatefriendvoidbar(){}};intmain(){foof0;foof1;}使用clang++编译没有问题(只有两个“未使用的变量”警告)但给出以下错误tmp_002-11,14,gcc,clang.cpp:Ininstantiationof‘structfoo’:tmp_002-11,14,gcc,clang.cpp:27:12:requiredfromheretmp_002-11,14,gcc,clang.cpp:20:16:erro

c++ - 当你是 friend 时,为什么 GCC 不允许从私有(private)嵌套类继承?

问同样的问题:为什么GCC允许从私有(private)嵌套类继承?对于非模板类,它允许从私有(private)嵌套类继承,如果它是一个friend,但不是模板类。是错误吗?templateclassInheritFromBaseMember:publicBase::MemberPrivate//error{usingPrivateMember=typenameBase::MemberPrivate;//worksfine};classMyBase{friendclassInheritFromBaseMember;//anothertrytodeclareitfriendtemplate

c++ - VS2015 和 clang 编译此代码,但 g++ 拒绝它。哪一个是正确的?

VS2015和clang编译这段代码,但是g++rejectsit.namespaceA{structB{friendvoidf();};}voidA::f(){}intmain(){}我认为g++是正确的,因为7.3.1.2/3中的注释:Ifafrienddeclarationinanon-localclassfirstdeclaresaclass,function,classtemplateorfunctiontemplate97thefriendisamemberoftheinnermostenclosingnamespace.Thefrienddeclarationdoesno

c++ - 选择声明模板友元的语法背后的基本原理是什么?

声明模板函数friends涉及一些非常不直观的语法,即使对于C++也是如此!选择额外的语法背后的基本原理是什么?需要吗?使用template不是更有意义吗?关键字?对于那些不知道这一点的人,这里有一个你可能会尝试做的例子:templateclassFoo{intx;friendvoidbar(Foo);};templatevoidbar(Foof){std::cout如果您尝试调用bar(Foo()),您将收到链接器错误。要解决这个问题,你必须转发声明bar(因此也是Foo)然后粘贴一个奇怪的位置在好友声明中。templateclassFoo;templatevoidbar(Foo);

c++ - 更正友元定义以授予 std::map 对私有(private)默认构造函数的访问权限

我正在开发一个使用结构的库,该结构不应具有该库用户可访问的默认构造函数。structExample{Example(intx);private:Example();};在库中,std::map需要默认构造函数来创建新条目。该库非常小心地在使用默认构造函数的任何地方实际放置值。库使用映射来存储这些结构,如下所示:std::mapdata;检查HEREFORACOMPLETEEXAMPLE在ideOne中。我想阻止库的用户使用默认构造函数。我如何与std::map、std::pair和/或std::tuple交friend以允许std::map使用此默认构造函数?friendclassst

c++ - "friend struct A;"和 "friend A;"语法有什么区别?

做和做有什么区别:structA;structB{friendstructA;};和structA;structB{friendA;};第二部分省略struct是什么意思? 最佳答案 不同的是,如果你写friendA;,A必须是一个已知的类型名,也就是说它必须在之前声明。如果你写friendstructA;,这本身就是A的声明,所以不需要事先声明:structB{friendstructA;};//OK虽然有一些微妙之处。例如,friendclass/structA在类B的最内层封闭命名空间中声明类A(感谢CaptainObvlio

c++ - 如何使 lambda 成为类的友元?

比方说,我有一个类:classA{inta;};我有一个lambda:autofunction=[](A*a){a->a;//有没有办法在lambda中使用私有(private)成员/方法?-没有必要将指针传递给lambda-它可能是捕获或其他东西。欢迎所有合理的方案。 最佳答案 您可以通过创建返回lambda函数的友元函数来实现。它继承了好友访问权限:structA{friendstd::functionf();private:inti;voidtest(){std::coutf(){return[](A&a,inti){a.i=

c++ - 一个类的方法中的本地类是这个类的 friend 吗?

我有一个外部类A。它有一个方法A::fun。在这个方法中,它有一个本地或内部类B。我的问题是:B是A的friend吗?我认为不是。这样对吗?如果是这样,我认为让B类成为A的friend是非常有益的,因为B可以访问A's私有(private)和protected成员。而且,由于B在方法中是本地的,其他人无法访问它,因此作为A的友元是安全的。如何让B访问A的私有(private)和protected成员? 最佳答案 不,他们不是friend。但是局部类对函数外部的名称具有与函数本身相同的访问权限。标准说:9.8Localclassdec

C++ 全局外部 "C" friend 无法访问命名空间类上的私有(private)成员

请考虑代码:#includeusingnamespacestd;extern"C"voidfoo(void);namespaceA{templateclassBar{private:friendvoid::foo(void);staticvoidprivate_func(intn);};templatevoidBar::private_func(intn){cout::private_func("::private_func(1);}intmain(){coutG++给出:>g++-Wall-oextern_cextern_c.cppextern_c.cpp:Infunction‘vo