C++ : protections and freedom...

K

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
 
A

Alf P. Steinbach

* 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.
 
S

Salt_Peter

kk said:
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.
 
L

Luke Meyers

kk said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top