473,395 Members | 1,977 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,395 software developers and data experts.

C++ : protections and freedom...

kk
I am trying to compile a list of items in the C++ language where it
offers some protections from the compiler that prevent the user from
making mistakes and in other areas total freedom to do what you want at
your own risk. For example, I would say the const keyword is an
overloaded keyword that offers protection on various fronts like const
variables(compiler prevents you from accidentally modifying these
variables), const member functions( compiler prevents you from
accidentally modifying the object on which the method is invoked), the
dynamic_cast ( a mechanism through which you can check the safety of
you casting operation), encapsulation through use of scope modifiers to
prevent users from accidentally modifying private data, etc.

On the other hand, the freedom to do anything at your own risk is
provided by way of the ability to directly access raw memory through
pointers, usage of reinterpret_cast to cast one type to a different
type, placement new and delete to specifically play around with
memory, etc.

Need help to compile more of these.

thanks

May 29 '06 #1
3 1520
* kk:
I am trying to compile a list of items in the C++ language where it
offers some protections from the compiler that prevent the user from
making mistakes and in other areas total freedom to do what you want at
your own risk. For example, I would say the const keyword is an
overloaded keyword that offers protection on various fronts like const
variables(compiler prevents you from accidentally modifying these
variables), const member functions( compiler prevents you from
accidentally modifying the object on which the method is invoked), the
dynamic_cast ( a mechanism through which you can check the safety of
you casting operation), encapsulation through use of scope modifiers to
prevent users from accidentally modifying private data, etc.

On the other hand, the freedom to do anything at your own risk is
provided by way of the ability to directly access raw memory through
pointers, usage of reinterpret_cast to cast one type to a different
type, placement new and delete to specifically play around with
memory, etc.

Need help to compile more of these.


The greatest and first protection in C++ is strong static type checking,
across compilation unit, which was added on top of C.

Note that Java, touted as a much safer language than C++, does not have
full strong static type checking across compilation units. At least,
Java didn't have that strong, global static type checking four or five
years ago. Who knows what it has today.

The greatest freedom in C++, as in C, is the use of pointer arithmetic
and void pointers.

However, there is a third category which might be much more interesting
in the end.

Namely the areas where current C++ supports the compiler's possible
1970's style optimization, to the detriment of well-defined'ness,
essentially trading correctness for no faster execution (because that's
the practical result today); one primary example is the free order of
evaluation for arguments to most built-in operators.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 29 '06 #2
kk wrote:
I am trying to compile a list of items in the C++ language where it
offers some protections from the compiler that prevent the user from
making mistakes and in other areas total freedom to do what you want at
your own risk. For example, I would say the const keyword is an
overloaded keyword that offers protection on various fronts like const
variables(compiler prevents you from accidentally modifying these
variables), const member functions( compiler prevents you from
accidentally modifying the object on which the method is invoked), the
dynamic_cast ( a mechanism through which you can check the safety of
you casting operation), encapsulation through use of scope modifiers to
prevent users from accidentally modifying private data, etc.
type-checking, namespaces and references should be on that list.

On the other hand, the freedom to do anything at your own risk is
provided by way of the ability to directly access raw memory through
pointers, usage of reinterpret_cast to cast one type to a different
type, placement new and delete to specifically play around with
memory, etc.

Need help to compile more of these.

thanks

May 29 '06 #3
kk wrote:
I am trying to compile a list of items in the C++ language where it
offers some protections from the compiler that prevent the user from
making mistakes and in other areas total freedom to do what you want at
your own risk.


I totally agree that const is a great example of this. I would add
template metaprogramming to the list; because it enables compile-time
computation, the number of execution paths at runtime is diminished,
which makes it less likely to miss an important path in testing.
That's the thing with generic code -- it's harder to write, but once it
compiles you have a lot more confidence that it works correctly,
because the compiler does so much checking (that's *why* it's harder to
write -- you're catching more bugs at compile-time!). Much like const,
this is a powerful mechanism for compile-time correctness.

The face that the (non-C-style) casting operators are so ugly is
another safety mechanism -- the ugly syntax discourages you from using
them, and also makes them easier to find by a simple search. This may
sound a little perverse, but it's actually part of why they were
designed the way they are.

In terms of freedom, several items occur to me:
* Operator overloading
* Function overloading
* Template specialization
* *Partial* template specialization
* Templates, period.
* Namespaces (don't like std::sort? Write your own!)

I don't know that all of these are the sort of thing that will bite you
at runtime if you screw them up, but the are certainly remarkable
degrees of freedom. C++'s strong static type checking (already
mentioned by Alf Steinbach) is pretty good at helping you catch your
mistakes early, so that they *don't* bite you, without paying any
runtime cost.

Luke

May 29 '06 #4

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

Similar topics

4
by: Shelly | last post by:
This is driving me crazy and makes no sense. Any suggestions will be appreciated. I upload an image to a staging area. I have written software to look at the images, and if accepted it is...
68
by: Grant Edwards | last post by:
Let's say I use a GPL'd python module (e.g. something installed in site-packages) in an application. Let's also say I use py2exe to package and distribute said application. Is what I'm...
79
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current...
24
by: Mary Rosemount | last post by:
http://www.studentsforacademicfreedom.org/index.html
3
by: envyisevil | last post by:
This article is dedicated to: A man I met, with most of his body heavily burned, simply because he was a Chinese during the 1998 May Riot in Indonesia. Lots of capitalists that are slaughtered...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.