473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Size_t & mutable

Hi,

Honestly, what is this 'size_t'?

And while I'm at it - what is a 'mutable' var.?
I've understood it somehow makes a const a non-const,
but what's the point of that?

Short explanations? Links? FAQ?

Thanks
-- Pelle
Jul 23 '05 #1
5 3336
Pelle Beckman wrote:
Hi,

Honestly, what is this 'size_t'?
An unsigned integer type used by the standard library to represent
sizes or indices. Many functions in the standard library take or return
size_t's and you should always use the same type. For example,

void f(std::vector<string> &v)
{
std::size_t size = v.size();

std::size_t index = 10;
std::cout << v[index];
}

Personnaly, I use std::size_t very often in my programs, not only in
conjuction with the standard library.
And while I'm at it - what is a 'mutable' var.?
I've understood it somehow makes a const a non-const,
but what's the point of that?


Allows an object to be logically const. An object is logically const
when its visible state does not change. For example, you may cache some
data for faster access. Accessing this data may modify the cache, and
this is ok even for const objects, since the cache does not modify the
object's visible state. Making the cache mutable allows a const member
function to modify it.

See
http://www.parashift.com/c++-faq-lit...html#faq-18.13
for some more infos. IIRC, Scott Meyer's [More] Effective C++ has an
item on that, but I cannot give specific references.
Jonathan

Jul 23 '05 #2
Jonathan Mcdougall wrote:
void f(std::vector<string> &v)
{
std::size_t size = v.size();

std::size_t index = 10;
std::cout << v[index];
}

Personnaly, I use std::size_t very often in my programs, not only in
conjuction with the standard library.


Would you go as far as

for(std::size_t i=0; i<max; ++i) array[i]*=2;

rather than

for(int i=0; i<max; ++i) array[i]*=2;
or
for(unsigned int i=0; i<max; ++i) array[i]*=2;

cheers,
- J.
Jul 23 '05 #3

"Jacek Dziedzic" <jacek@tygrys._to_wytnij_.net> wrote in message
news:db**********@node2.news.atman.pl...
Jonathan Mcdougall wrote:
void f(std::vector<string> &v)
{
std::size_t size = v.size();

std::size_t index = 10;
std::cout << v[index];
}

Personnaly, I use std::size_t very often in my programs, not only in
conjuction with the standard library.


Would you go as far as

for(std::size_t i=0; i<max; ++i) array[i]*=2;

rather than

for(int i=0; i<max; ++i) array[i]*=2;
or
for(unsigned int i=0; i<max; ++i) array[i]*=2;

cheers,
- J.


I would go as far to use size_t for indexing a vector. This way the compiler
wouldn't give a warning (as it will do if you use a signed int.)

Regards,
Ben
Jul 23 '05 #4
| Personnaly, I use std::size_t very often in my programs, not only in
| conjuction with the standard library

Jonathan, I realize the word 'very often', nonetheless, I too try to
follow the same approach but what do you do for the case where you're
faced with vendor API's that expects unsigned. One obvious answer is
to use an unsigned. This way it rids the compiler warnings related to
potential conversions from unsigned long (size_t may be unsigned long
on said platform) to unsinged int - 'loss of data'. I'd like to belive
vendors should just use size_t in this case but ....

Jul 23 '05 #5
ma******@pegasus.cc.ucf.edu wrote:
| Personnaly, I use std::size_t very often in my programs, not only in
| conjuction with the standard library

Jonathan, I realize the word 'very often', nonetheless, I too try to
follow the same approach but what do you do for the case where you're
faced with vendor API's that expects unsigned.
std::size_t is unsigned. Do you mean unsigned int?
One obvious answer is
to use an unsigned. This way it rids the compiler warnings related to
potential conversions from unsigned long (size_t may be unsigned long
on said platform) to unsinged int - 'loss of data'. I'd like to belive
vendors should just use size_t in this case but ....


Use the type which suits the circumstances. Try to use a type with
which you won't need any casts. If a function expects a signed integral
and you are using an unsigned integral, it may mean that the origin is
not compatible with the destination. In most cases though, converting
an unsigned (say, an index) to a signed integral is not too much
trouble. Every case is different.

If a function expects an unsigned int and you have a std::size_t which
is a typedef for an unsigned long, just make sure you never overflow
the unsigned int. Using 3rd party libraries usually means adaptation
and tradeoffs.
Jonathan

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
12
by: jeffc | last post by:
Can anyone point me to a web site that gives technical reasons that pass by value is preferred over pass by reference when the value is not to be changed? Actually it could be any built-in "small"...
18
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities?...
12
by: Kjetil Kristoffer Solberg | last post by:
What is a mutable struct? regards Kjetil Kristoffer Solberg
12
by: Water Cooler v2 | last post by:
Are JavaScript strings mutable? How're they implemented - 1. char arrays 2. linked lists of char arrays 3. another data structure I see that the + operator is overloaded for the string class...
4
by: jansenh | last post by:
hi comp.lang.python. I need some newbe advice on idiomatic use of Python dictionaries. I have service with a dictionary which holds a bunch of objects as values, and an ID as key to each...
2
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on...
24
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.