草庐IT

pointer_traits

全部标签

c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

收到此错误,我很确定它在operatorvoidCRational::print()const{print(cout);}voidCRational::print(ostream&sout)const{if(m_denominator==1)cout 最佳答案 您需要通过引用而不是值返回ostream。它试图调用构造函数。也可以传递'a'作为引用:ostream&operator我还注意到打印方法可能是错误的。它有sout作为流的名称传递,但随后直接使用cout实现。应该是voidCRational::print(ostream&s

c++ - C++11 <type_traits> 模板参数类型推导失败

我正在尝试了解如何使用C++(11).这是我的简单测试程序#includetemplateinlineUadd(typenamestd::enable_if::value,U>::typea,typenamestd::enable_if::value,S>::typeb){returna+b;}intmain(intargc,constchar*argv[],constchar*envp[]){unsignedintui;inti;autoa=add(ui,i);return0;}当使用GCC4.8.1编译时,它会出错/home/per/f.cpp:Infunction‘intmain

c++ - 为什么 type_traits 是用专门的模板结构而不是 constexpr 实现的?

标准将它们指定为模板struct而不是简单的bool值constexpr有什么原因吗?在一个可能会在主要问题的良好答案中得到回答的附加问题中,如何对非结构版本执行enable_if操作? 最佳答案 一个原因是constexpr函数无法提供嵌套的type成员,这在某些元编程情况下很有用。为了清楚起见,我不是只谈论产生类型但显然不能产生类型的转换特征(如make_unsigned)constexpr功能。所有类型特征都提供了这样一个嵌套的type成员,甚至是一元类型特征和二进制类型特征。例如is_void::type是false_typ

c++ - c++ : error: must use '.*' or '->*' to call pointer-to-member function in function 中的函数指针

代码片段如下。无法理解为什么会出现此错误。voidSipObj::check_each_field(){map::iteratormsg;stringstr;charname[20];boolres=false;sscanf(get_payload(),"%s%*s",name);LOGINFO(lc())second;res=(this).*sip_field();}}typedefbool(SipObj::*sip_field_getter)();staticmapsip_field_map;sip_field_getter是函数名的占位符 最佳答案

c++ - 无法将 "member pointer to derived class"转换为 "member pointer to base class"

使用指向基类的指针调用类的虚成员函数当然是C++中非常常见的事情。所以我觉得很奇怪,当你有一个成员指针而不是一个普通指针时,似乎不可能做同样的事情。请考虑以下代码:structB{virtualvoidf();};structD:B{virtualvoidf();};structE{Bb;Dd;};intmain(){Ee;//Firstwithnormalpointers:B*pb1=&e.b;//OKB*pb2=&e.d;//OK,BisabaseofDpb1->f();//OK,callsB::f()pb2->f();//OK,callsD::f()//Nowwithmember

C++11/14 : How to remove a pointer-to-member from a type?

在C++11或C++1y/14中:给定一个TC::*形式的指向成员类型的值,我想得到指向类型例如:#includeusingnamespacestd;structT1{staticvoidf(){coutstructV;templatestructV{staticconstexprT1U1::*pm=&U1::x;};templatestructV{staticconstexprT2U2::*pm=&U2::x;};templatevoidf(Wpm){typedef???T;T::f();}intmain(){f(V::pm);f(V::pm);}有没有办法做到这一点?上面的???是

c++ - 为什么是非法的: copying vector of pointers into a vector of pointers to constants

问题以下代码无法在C++11(或C++14)中编译。我理解编译器的错误输出,但为什么标准不允许?//main.cpp#includeintmain(void){doublea=3.0;doubleb=3.0;//Itworkswithmerepointersconstdouble*ptrToConst=&a;/***/double*ptrToObj=&a;//ptrToObj=ptrToConst;//Illegal:that'sunderstandable…ptrToConst=ptrToObj;//Works//Butthesamedoesn'tworkwithvectorstop

c++ - C++ 中的 int[pointer-to-array] 是标准的吗?

这个问题在这里已经有了答案:Witharrays,whyisitthecasethata[5]==5[a]?(19个回答)Whydoesx[y]==y[x]inc++?[duplicate](3个答案)关闭8年前。据我所知,可以编写以下代码:char*a=newchar[50];for(inti=0;i它编译。有用。它完全与相同char*a=newchar[50];for(inti=0;i仅仅是因为:a[b]默认情况下作为宏*(a+b)实现,两个代码示例都有效的事实只是一个意外/特定于编译器它已在某处标准化,此类算法的结果在每个平台上都应该相同假设加法应该是可交换的是合理的,但如果我们

c++ - std::is_member_function_pointer 不适用于 noexcept 成员函数

我在使用std::is_member_function_pointer时遇到问题。据我所知,在给定noexcept成员函数时它不起作用。我在标准中找不到任何声明它不适用于noexcept合格成员函数的内容。问题示例:#includeclassA{public:voidmember()noexcept{}};intmain(){//failsatcompiletimeifA::memberisadatamemberandnotafunctionstatic_assert(std::is_member_function_pointer::value,"A::memberisnotamemb

c++ - 无法访问类 'std::basic_ios<_Elem,_Traits>' 中声明的私有(private)成员

这个特定方法有问题,不知道如何解决!我得到的错误是上面的:"errorC2248:'std::basic_ios::basic_ios':cannotaccessprivatememberdeclaredinclass'std::basic_ios'C:\ProgramFiles\MicrosoftVisualStudio10.0\VC\include\ostream604"我的方法是:ostreamoperator在标题中:friendstd::ostreamoperator关于如何解决这个问题的任何想法?我认为这与通过引用而不是值传递有关...但我有点困惑!