草庐IT

lifted-operators

全部标签

c++ - operator<< with boost::variant 是如何实现的

我明白boost::variant是这样实现的templatestructvariant{std::aligned_union::typebuffer;....};我们如何制作operator对于像这样的结构,打印缓冲区中存储的类型并将其传递给operator对于cout?为此,我们需要知道存储在缓冲区中的元素的类型,对吧?有没有办法知道这一点?此外,我正在寻找对此类实现的解释(如果存在的话)。不仅仅是它的存在以及我如何使用它。 最佳答案 Boost有一个apply_visitor函数,它接受一个通用函数对象并将变量的类型传递给它。

c++ - C/C++ : Bitwise operators on dynamically allocated memory

在C/C++中,是否有一种简单的方法可以将按位运算符(特别是左移/右移)应用于动态分配的内存?例如,假设我这样做了:unsignedchar*bytes=newunsignedchar[3];bytes[0]=1;bytes[1]=1;bytes[2]=1;我想要一种方法来做到这一点:bytes>>=2;(那么“字节”将具有以下值):bytes[0]==0bytes[1]==64bytes[2]==64为什么值应该是这样的:分配后,字节如下所示:[00000001][00000001][00000001]但我希望将字节视为一长串位,如下所示:[000000010000000100000

c++ - 在 std::operator << [With _Traits = std::char_traits<char> ] 中不匹配 'operator <<'

我有一个带有字符串转换运算符的Foobar类:#includeclassFoobar{public:Foobar();Foobar(constFoobar&);~Foobar();operatorstd::string()const;};我尝试这样使用它://C++源文件#include#include#include"Foobar.hpp"intmain(){Foobarfb;std::stringstreamss;ss我是否需要为Foobar显式创建一个运算符那么为什么会出现这个错误呢?我错过了什么?[编辑]我刚刚发现,如果我将发生错误的行更改为:ss编译成功。呃……!为什么编译器

c++ - 为什么 "const T*"在 "void*"中被简单地转换为 "operator delete"?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Deletingapointertoconst(Tconst*)voidoperatordelete(void*);...constchar*pn=newchar,*pm=(char*)malloc(1);deletepn;//allowed!!free(pm);//errorDemo.可以理解,free()是一个函数,所以constvoid*不能转换为void*。但为什么在operatordelete(默认或重载)的情况下允许这样做?它在功能上不是错误的构造吗?

c++ - operator>> 适用于 Visual C++ 2010 但不适用于 Linux 上的 G++

我有以下问题:我的代码适用于VisualC++2010,但是当我在Linux上编译它时,它被编译了,但是有些东西不起作用:这是我的Vector输入operator>>:istream&operator>>(istream&in,Vector&x){chara;in.sync();a=in.get();//getsthe'['for(inti=0;i>x._vector[i];if((i+1)!=x._n)a=in.get();//getsthe','}in>>a;//getsthe']'returnin;}_vector指向一个Complex数组,Complex的operator>>工

c++ - std::vector::operator[] 不测试类型

我有一个这样的结构:structVrtxPros{longidx;std::vectorpros;VrtxPros(constlong&_idx=-1,conststd::string&val=""):idx(_idx){if(!val.empty()&&val!="")pros.push_back(val);}};后来在代码中我这样使用它:longidx=1234;VrtxProsvp(2134,std::string("-1"));if(margin)vp.pros[0]=idx;编译器对此没有问题。我想知道,因为运营商应该提供引用。我couldnotfindstd::string

c++ - 使用转换的 operator+ 的模糊重载

我得到了一个转换konw->int,double->konw的类:classkonw{doublere,im;public:konw():re(0.0),im(0.0){}konw(doubler,doublei=0.0):re(r),im(i){}operatorint(){returnre;}konwoperator+(konwa){konwwynik;wynik.re=re+a.re;wynik.im=im+a.im;returnwynik;}};主要是我使用重载的operator+测试这些转换konwzesp(3.1,0.6);intssuma=zesp+6;编译器在处理最后一

C++ 编译错误 - ‘operator=’ 不匹配

下面的代码编译没有问题classMyClass{public:MyClass(){std::cout但是当我将赋值运算符参数更改为非常量时编译器打印错误:MyClass&operator=(MyClass&m){std::cout我想知道原因。提前致谢。 最佳答案 因为MyClass&使没有const的operator=不是正确的赋值运算符。它必须是operator=(constMyClass&)(或operator=(MyClass)但不要那样做,除非你知道自己在做什么,copy-&-swap。..).否则,您的代码d=MyCla

c++ - boost spirit : Difference between operators "%=" and "="

我不明白这两个运算符之间的区别。让我们举一个例子,将像"AA,BB,CC,DD"这样的输入解析成字符串vector。namespaceqi=boost::spirit::qi;classmy_grammar:publicqi::grammar{public:my_grammar():base_type(start){usingqi::_1;usingqi::char_;start=*(char_-qi::lit(','));}qi::rulestart;};据我所知,a%=b等同于a=b[_val=_1]。这很清楚。但另一方面,解析器*(char_-qi::lit(','))具有std

c++ - 为什么继承的 protected operator=() 具有公共(public)访问权限

声明为protected的重载运算符=对于继承父类作为public的子类是公开可访问的。#includeclassA{public:A(charc):i(c){}chari;protected:A&operator=(constA&rdm){std::cout编译时没有错误:$g++-Wall-otest_operator~/test_operator.cpp$./test_operatora.i==aaccessingoperator=()a.i==x直接使用A是编译不过的。operator=()以外的任何其他运算符重载都不会编译。使用g++4.4.7和7.3.0以及c++98和c+