jjzjj

命名空间中的 C++ 前向声明和友元

根据C++标准ISO/IEC14882:2003(E)中的7.3.1.2命名空间成员定义Everynamefirstdeclaredinanamespaceisamemberofthatnamespace.Ifafrienddeclarationinanon-localclassfirstdeclaresaclassorfunction(thisimpliesthatthenameoftheclassorfunctionisunqualified)thefriendclassorfunctionisamemberoftheinnermostenclosingnamespace.//As

c++ - 获取内联定义的友元函数的地址

考虑以下代码:#includestructfoo{friendvoidbar(foo){}voidfoobar(){std::coutgcc给我这个错误:main.cpp:Inmemberfunction'voidfoo::foobar()':main.cpp:7:23:error:'bar'wasnotdeclaredinthisscopestd::cout那是因为bar是类本身定义的友元函数,使其在全局命名空间中不可见。访问它的唯一方法是通过ADL,但我还没有找到使用ADL获取bar地址的方法。所以我的问题是,如何获取bar的地址?除了在foo之外定义bar之外,还有其他方法吗?

c++ - 默认参数位置的 Lambda 无法访问友元成员。这是编译器错误吗?

我正在尝试编译一个使用超现代神秘编码技术编写的程序。这些技术非常先进,GCC和Clang可以工作,但VisualStudio2017会抛出错误。现在我想知道VisualStudio是否做对了。考虑以下程序:#include#includeclassA{public:A(inti):foo(i){}private:intfoo;friendclassB;};classB{public:voidprintFooFromA(constA&a,std::functionprinter=[](constA&a){std::coutVisualStudio抛出错误C2248:“A::foo”:无法

友元函数的 C++ 内联定义

在C++标准的当前草案中(2019年3月)[class.friend]p.6状态(强调我的):Afunctioncanbedefinedinafrienddeclarationofaclassifandonlyiftheclassisanon-localclass([class.local]),thefunctionnameisunqualified,andthefunctionhasnamespacescope.[...]“函数具有命名空间作用域”是什么意思?我能想到的函数没有命名空间作用域的唯一情况如下:structA{staticvoidf();structB{friendvoi

c++ - 友元函数和静态数据成员

如何在friend的函数体内对using名称执行非限定名称查找?让我们考虑以下代码:#includevoidfoo();classA{friendvoidfoo(){std::coutDEMON4296::7.3.1.2/3[namespace.memdef]中的标准声明:Ifafrienddeclarationinanon-localclassfirstdeclaresaclass,function,classtemplateorfunctiontemplatethefriendisamemberoftheinnermostenclosingnamespace.所以,我预计不合格的名

c++ - 限制多个模板参数友元函数可访问的类实例的范围

我想知道我的目标是否可行。我有一个这样的类#includetemplateclassClass;templateClassf(Class&C,constClass&D);templateclassClass{protected://thiscouldbeprivateTm_t;public:Class():m_t(T()){}Class(Tt):m_t(t){}T&getT(){returnm_t;}templatefriendClassf(Class&C,constClass&D);};templateClassf(Class&C,constClass&D){C.m_t+=D.m_t

c++ - 如何定义在两个类之外的模板类内部的非模板类中声明的友元函数?

我找到了“如何在其声明之外定义模板类的友元模板函数”(SO/cppreference),但如果我们在混合中添加另一个内部非模板类,该怎么做?即如何(外部)定义operator在classInternal中声明来自以下示例:#includetemplateclassExternal{public:explicitExternal(Tinitial):value{initial}{}classInternal{public:Internal(constExternal&e):internal_value{e.value}{}private:friendstd::ostream&operat

c++ - 命名空间内类的友元函数

关于这段代码,我有两个问题:namespaceA{classwindow;}voidf(A::window);namespaceA{classwindow{private:inta;friendvoid::f(window);};}voidf(A::windowrhs){std::cout1)为什么我需要通过执行::f(window)将窗口类中的成员函数f限定为全局的?2)为什么在这种特殊情况下我需要预先声明函数f(A::window),而当类未在命名空间内定义时,可以在函数声明为friend之后声明函数. 最佳答案 当您将f()声

c++ - 在 C++ 中将 main() 声明为友元函数

我有一个类,我只希望客户为每个进程创建一个对象。除了单例,更好的方法(我相信)是告诉客户只在main()中创建它们。因此,一种自然的强制措施是将构造函数设为私有(private)并将main()作为友元。它是这样工作的:classA{friendintmain(int,char**);A(){}};intmain(int,char**){Aa;}但是当我需要将类A放入命名空间时它会中断:namepacens{classA{friendintmain(int,char**);A(){}};}intmain(int,char**){ns::Aa;}问题是作用域:编译器现在认为friendi

C++友元类

我意识到在C++中有很多关于友元类的问题。不过,我的问题与特定情况有关。给定以下代码,以这种方式使用friend是否合适?classSoftware{friendclassSoftwareProducer;SoftwareProducer*m_producer;intm_key;//OnlyproducerscanproducesoftwareSoftware(SoftwareProducer*producer):m_producer(producer){}public:voidbuy(){m_key=m_producer->next_key();}};classSoftwarePro