C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Subscript operator overloading with vectors

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
olson_ord@yahoo.it
Guest





PostPosted: Fri Jan 27, 2006 12:14 pm    Post subject: Subscript operator overloading with vectors Reply with quote



Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here's a simpler version of it.

-------------------- main.cpp---------------------------

# include <iostream>
# include <vector>
# include <cassert>

class ShiftRegister {
public:
ShiftRegister(unsigned size) : _reg(size) {

}

bool& operator[](unsigned ix) {

return _reg[ix];
}

const bool& operator[](unsigned ix) const {

return _reg[ix];
}

private:
std::vector<bool> _reg;
};


int main(int argc, char* argv[]) {

ShiftRegister sftreg(2);

}


------------------ end of main.cpp ----------------

The above contains two definitions of the subscript operator one
ordinary and one constant. Compiling this I get
1. For the ordinary or variable subscript operator
error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](ix)'
to `bool&'

2. For the constant subscript operator
warning: returning reference to temporary

I think I am missing something here, but looking in books or on the
internet does not make me understand what I am doing wrong i.e. they
seem to be doing the same thing (but with arrays or pointers - not with
vectors.)

I hope someone has some ideas on how to get this to work.
Thanks a lot.
O.O.

Back to top
Ben Radford
Guest





PostPosted: Fri Jan 27, 2006 12:22 pm    Post subject: Re: Subscript operator overloading with vectors Reply with quote



[email]olson_ord (AT) yahoo (DOT) it[/email] wrote:
Quote:
Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here's a simpler version of it.

-------------------- main.cpp---------------------------

# include <iostream
# include # include
class ShiftRegister {
public:
ShiftRegister(unsigned size) : _reg(size) {

}

bool& operator[](unsigned ix) {

return _reg[ix];
}

const bool& operator[](unsigned ix) const {

return _reg[ix];
}

private:
std::vector };


int main(int argc, char* argv[]) {

ShiftRegister sftreg(2);

}


------------------ end of main.cpp ----------------

The above contains two definitions of the subscript operator one
ordinary and one constant. Compiling this I get
1. For the ordinary or variable subscript operator
error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](ix)'
to `bool&'

2. For the constant subscript operator
warning: returning reference to temporary

I think I am missing something here, but looking in books or on the
internet does not make me understand what I am doing wrong i.e. they
seem to be doing the same thing (but with arrays or pointers - not with
vectors.)

I hope someone has some ideas on how to get this to work.
Thanks a lot.
O.O.


My first thoughts on this are that it's to do with the specialisation of
the std::vector template for bools (ie std::vector<bool> is a vector of
bits not bools which the standard says must be at least 1 byte). Does
the code work if you use another primitive type?

Back to top
Ben Radford
Guest





PostPosted: Fri Jan 27, 2006 12:32 pm    Post subject: Re: Subscript operator overloading with vectors Reply with quote



Ben Radford wrote:
Quote:
olson_ord (AT) yahoo (DOT) it wrote:

Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here's a simpler version of it.

-------------------- main.cpp---------------------------

# include <iostream
# include # include
class ShiftRegister {
public:
ShiftRegister(unsigned size) : _reg(size) {

}

bool& operator[](unsigned ix) {

return _reg[ix];
}

const bool& operator[](unsigned ix) const {

return _reg[ix];
}

private:
std::vector };


int main(int argc, char* argv[]) {

ShiftRegister sftreg(2);

}


------------------ end of main.cpp ----------------

The above contains two definitions of the subscript operator one
ordinary and one constant. Compiling this I get
1. For the ordinary or variable subscript operator
error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](ix)'
to `bool&'

2. For the constant subscript operator
warning: returning reference to temporary

I think I am missing something here, but looking in books or on the
internet does not make me understand what I am doing wrong i.e. they
seem to be doing the same thing (but with arrays or pointers - not with
vectors.)

I hope someone has some ideas on how to get this to work.
Thanks a lot.
O.O.


My first thoughts on this are that it's to do with the specialisation of
the std::vector template for bools (ie std::vector<bool> is a vector of
bits not bools which the standard says must be at least 1 byte). Does
the code work if you use another primitive type?

To elaborate, the bools in std::vector<bool> don't exist as bools. When
you access the vector you get given a temporary bool created by
examining whether the relative bit is on or off. Since it is only
temporary you can't return a reference to it. I hope that's a bit
clearer than my initial reply =)

Back to top
olson_ord@yahoo.it
Guest





PostPosted: Fri Jan 27, 2006 2:32 pm    Post subject: Re: Subscript operator overloading with vectors Reply with quote

Very, very good guess Ben. I never realised or even thought about it.
Yes I tried to compile my program with this bool replaced by int - and
it works.
However I actually need bool 's. (If you have any idea about what a
Shift Register is - you would know why I don't really require
integers.) I also wanted to optimize this for speed. I would think
about this problem and see what can be done - now that I at least know
what the problem is.
Thanks once again.
O.O.

Back to top
Ben Pope
Guest





PostPosted: Fri Jan 27, 2006 2:37 pm    Post subject: Re: Subscript operator overloading with vectors Reply with quote

[email]olson_ord (AT) yahoo (DOT) it[/email] wrote:
Quote:
Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here's a simpler version of it.

<snip std::vector

Quote:
I hope someone has some ideas on how to get this to work.
Thanks a lot.

Are you aware of std::bitset?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...

Back to top
roberts.noah@gmail.com
Guest





PostPosted: Fri Jan 27, 2006 2:43 pm    Post subject: Re: Subscript operator overloading with vectors Reply with quote


[email]olson_ord (AT) yahoo (DOT) it[/email] wrote:
Quote:
Very, very good guess Ben. I never realised or even thought about it.
Yes I tried to compile my program with this bool replaced by int - and
it works.
However I actually need bool 's. (If you have any idea about what a
Shift Register is - you would know why I don't really require
integers.)

Use chars...there really isn't a diff anyway.


Back to top
Marcus Kwok
Guest





PostPosted: Fri Jan 27, 2006 3:02 pm    Post subject: Re: Subscript operator overloading with vectors Reply with quote

Ben Radford <benjamin.radford (AT) new (DOT) ox.ac.uk> wrote:
Quote:
Ben Radford wrote:
My first thoughts on this are that it's to do with the specialisation of
the std::vector template for bools (ie std::vector<bool> is a vector of
bits not bools which the standard says must be at least 1 byte). Does
the code work if you use another primitive type?

To elaborate, the bools in std::vector<bool> don't exist as bools. When
you access the vector you get given a temporary bool created by
examining whether the relative bit is on or off. Since it is only
temporary you can't return a reference to it. I hope that's a bit
clearer than my initial reply =)

Yes, for more information:

When Is a Container Not a Container?
http://www.gotw.ca/publications/mill09.htm

vector<bool> Is Nonconforming, and Forces Optimization Choice
http://www.gotw.ca/publications/N1185.pdf

vector<bool>: More Problems, Better Solutions
http://www.gotw.ca/publications/N1211.pdf

--
Marcus Kwok

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.