草庐IT

user-friendly

全部标签

c++ - 如何将模板函数声明为模板嵌套类的 friend ?

我怎样才能制作get封闭范围内的一个函数,可以访问outer::inner的私有(private)构造函数?templatestructouter{templateclassinner{inner(){}public:friendinnerget(outer&){return{};}};};intmain(){outerfoo;outer::innerbar=get(foo);}我尝试通过制作inner来宣布它不在类里面有一个templatefriendinnerget(outer&);但这也不起作用。 最佳答案 Ihavetrie

c++ - 为什么 Foo::inner Constexpr 不会链接,而 User Literal{Foo::inner Constexpr} 会链接?

考虑以下简单类,这些类是我根据在实际项目中遇到的问题设计的。Triple是一种与内部一起使用的快速样板类型constexprFoo类中的s:#includeclassTriple{public:friendstd::ostream&operator如果我再写一个main()使用公共(public)内部函数constexpr来自Foo,如下,会链接失败(使用g++4.7.0,在Windows7上通过mingw-x86-64):intmain(intargc,char**argv){usingstd::cout;usingstd::endl;cout$g++-otest-O3--std=c

c++ - 标准库predefined 'user-defined' literal "m"是在哪里定义的?

我在浏览C++CoreGuidlines时偶然发现了以下示例文档:Examplechange_speed(doubles);//bad:whatdoesssignify?//...change_speed(2.3);Abetterapproachistobeexplicitaboutthemeaningofthedouble(newspeedordeltaonoldspeed?)andtheunitused:change_speed(Speeds);//better:themeaningofsisspecified//...change_speed(2.3);//error:nouni

c++ - 通过继承C++成为 friend

假设我有两个类(class)Widget^|Window我还有另一个类应用程序:定义如下classApplication{public:...private:friendWidget;};这不会让Window访问Applicationsprotected和private成员。有没有一种方法可以在不将Window和任何后续“Widget”声明为Application的友元的情况下完成此操作? 最佳答案 不,这是不可能的。friendship不可继承。此外,friendship表示两个实体之间有意强耦合因此,如果您的设计确实需要如此强的

c++ - boost 测试 : catch user defined exceptions

如果我的代码中有用户定义的异常,我将无法进行Boost测试将它们视为失败。例如,BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MyTest,1)BOOST_AUTO_TEST_CASE(MyTest){//codewhichthrowsuserdefinedexception,notderivedfromstd::exception.}我收到一条通用消息:Caughtexception:....unknownlocation(0):....它不会将此错误识别为失败,因为它不是std::exception。所以它不遵守expected_failures条款

c++ - 白盒测试—— friend 还是预处理器?

假设我们有这样一个类:classTestee{public:voidFunc()private:voidauxFunc()};我们想对其进行白盒单元测试。您认为哪种方法更好?将测试类声明为测试类的friend?或者像这样使用预处理器:classTestee{public:voidFunc()#ifndefUNITTEST_SYMBOLprivate:#elifpublic:#endifvoidauxFunc()};稍后在测试文件中#defineUNITTEST_SYMBOL#include"Testee.h"#undefUNITTEST_SYMBOL那么,您认为哪种方法更好?或者,也许

c++ - 在 friend 的类中包含头文件

我想知道您是否必须在使用它作为友元的类中#include"Class1.h"。例如,授予Class1类权限的类的.h文件。classClass2{friendclassClass1;}您需要#include"Class1.h"还是没有必要?同样在Class2类中,永远不会创建或使用Class1对象。Class1只是操纵Class2而不是相反。 最佳答案 语法是:friendclassClass1;不,您不包含header。更一般地说,除非您实际上以某种方式使用类定义(例如,您使用该类的实例并且编译器需要知道其中的内容),否则您不需要

c++ - 如果在 user.hpp 中定义了 BOOST_NO_EXCEPTIONS,为什么不能编译 boost::shared_ptr

我有一个嵌入式系统,想在这个系统中使用boost,但需要禁用异常,因为我不想支付异常成本。boost给了一个user.hpp和可设置的宏选项BOOST_NO_EXCEPTIONS和BOOST_NO_EXCEPTION_STD_NAMESPACE,但是boost::shared_ptr不能编译(更准确的说,不能链接)如果定义了这两个宏。shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a):undefinedrefe

c++ - 运算符的 friend 特定模板实例化

我有一个类模板和一个需要访问其私有(private)字段的运算符模板。我可以交一个模板friend:templateclassA{intx;templatefriendbooloperator==(constA&a,constA&b);};templatebooloperator==(constA&a,constA&b){returna.x==b.x;}intmain(){Ax,y;x==y;return0;}但是有没有可能只制作operator==friendA而不是制作operator==A的friend? 最佳答案 如果fri

c++ - friend 类的部分模板特化?

我有一个简单的类X和一组模板化类Y。我希望第一个模板化参数恰好是X的所有类Y都成为X本身的friend。下面希望传达我想要的,但是friend语句给出了编译错误。templateclassY{};classX{public:X(intvalue):i(value){}constint&getI()const{returni;}private:inti;templatefriendclassY;};我不确定完全允许友元语句模板化(更不用说友元语句的部分模板化了)。有没有办法做到这一点?还是我无法一一列出所有friend?谢谢,马特 最佳答案