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 5 3361
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<s tring> &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
Jonathan Mcdougall wrote: void f(std::vector<s tring> &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.
"Jacek Dziedzic" <jacek@tygrys._ to_wytnij_.net> wrote in message
news:db******** **@node2.news.a tman.pl... Jonathan Mcdougall wrote:
void f(std::vector<s tring> &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
| 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 .... ma******@pegasu s.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 interchangeable in all
circumstances (as long as they're paired) so there's no overloading to
muddy the language. Of course there could be some interesting problems
with current code that doesn't make a distinction, but it would be dead
easy to fix...
|
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
def foo(self, data):
self.attr1=data
self.attr2.append(data)
The initialization of attr1 is obviously OK, all instances of Father
redefine it in the method foo. But the initialization of attr2 is wrong
|
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" type (short, bool, etc.)
|
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?
http://david.tribble.com/text/cdiffs.htm#C99-cpp-keyword
http://www.inf.uni-konstanz.de/~kuehl/c++-faq/const-correctness.html#faq-18.13
Regards,
Markus
|
by: Kjetil Kristoffer Solberg |
last post by:
What is a mutable struct?
regards
Kjetil Kristoffer Solberg
| |
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 and hence
it is possible to do:
txt += "foo bar";
|
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 object. Then I want to change an
objects state based on its key. The way I am doing this now is by using
'fromkeys' and copying my object over in a temporary dictionary, then
manipulating the object, and then I do an 'update' back to the main...
|
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 whether the result of
operator* is a reference or a constant reference."
As per this definition, on page 71 in this book, it is mentioned that
for 'set' and 'multiset', both the iterator and const_iterator types
are constant bidirectional types -...
|
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 finally, and painfully, fade
away than another one starts up.
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |