Somehow I never noticed until today that C++ supports nested classes. This surprised me because when I was learning C++ back in the '90s, I specifically remember nested classes being something that Object Pascal and Java had, but which C++ did not. I asked an old programmer friend about it and he concurred that he recalls C++ not having nested classes.
Is my recollection of C++ not having nested classes mistaken, or were they actually added to the standard at some point in the past fifteen years? I tried searching Google for information on this topic and I haven't come up with anything helpful yet.
It could also be that I'm thinking of nested functions, which Pascal certainly supports but C does not.
According to "A History of C++: 1979−1991" by Bjarne Stroustrup
Later, after many technical problems
and much discontent from users, nested
class scopes were re−introduced into
C++ in 1989 [Ellis,1990].
see page 28 in http://www.research.att.com/~bs/hopl2.pdf
Nested classes were added in CFront 3.0, released in 1993.
EDIT It goes back even earlier, as you can see in the table of contents to The Annotated C++ Reference Manual (1990).
were they actually added to the
standard at some point in the past
fifteen years?
C++ was first standardised in 1998 and this standard included nested classes in section 9.7. Since your talking early 90s in your question it is quite possible that whatever particular compiler you were using at the time did not provide support for them.
So Nested Classe were officially supported from 1998 onwards but could have been available (depending on your compiler) at any time prior to this, as alluded to in the answer from Ken Bloom.
Related
Is there a way to find out what the protection level of a member is?
I'm making a singleton base-class and I'd like to ensure that the child class declares its constructor/destructor as private/protected. How can I do this?
template<class c>
class singleton
{
static_assert(std::is_private<&c::c> // does this exist?
|| std::is_protected<&c::c>);
static_assert(std::is_private<&c::~c>
|| std::is_protected<&c::~c>);
};
There is a paper written that proposed this in 2018, (and later 2019). In fact, it appears it was circulated in June of 2019 as part of the Committee's proposal process. However, I'm not really sure what happened to it (other then I think I found another revision).
Some compilers might support it experimentally. If they did, I believe the header you would use is <experimental/reflect> (according to the paper, at least).
Otherwise, you're kind of stuck. It appears reflection was deferred to a later standard, so nothing like this really exists in C++ as it stands. Hopefully it will make it into C++23. But for now we have to wait.
Unfortunately, what you are trying to do is not possible. So far, C++ doesn't have reflection features that are powerful enough.
In reality, programmers use singleton classes without needing protection mechanisms like this, because C++ is not designed for maximum compile-time validation. There are a whole lot of other similar things you can't do in C++ — in other words, it is impossible for C++ to accommodate for every possible feature users may expect, especially those that are not generally considered useful. Reflection is proposed because many people find it helpful, but so far the equivalent capabilities have not been included in C++.
I recently learned about the wonderful memory management technique of RAII, which seems so much cleaner than the new/delete headache I learned in school years ago (I haven't looked at much C++ during the intervening years).
I'm trying to track down when this great technique was added to C++. Was it always there and I just missed the memo? What's the oldest version of the C++ standard which supports RAII?
[UPDATE: OK I realize now why this isn't an ideal SO question -- I have no practical way to verify whether any given answer is correct! Nonetheless I'm still very interested to learn the answer, and I assume the majority opinion will be true.
What I'm hearing is that it's always been there, which I don't doubt is true, but begs the question how come none of my old text books mention it? I just checked Meyers' Effective C++ 2nd edition which I have handy, and will check older texts tonight. Maybe the term was only recently coined, while the technique existed long before?]
When exactly the term "RAII" was coined, I'm not sure. But the technique itself existed in C++ from the moment of its invention, circa 1979.
The first version of the C++ ISO standard was published in 1998.
Any language that has scoped variables with automatic destructor calls is able to do RAII. C++ had this concept from the very beginning as calling destructor when execution leaves scope where variable is declared was in C++ from the very beginning.
Wikipedia's claim that Stroustrup invented RAII is backed by a reference to his book Design and Evolution of C++, published in 1994.
I don't have a copy to check, but it would seem to date from at least this time.
In the past I have seem a couple of people say C+ (with just one plus). I always thought that such persons were "ignorant" cos the language is called C++ not C+.
However, I was watching a youtube video were a guy said that there was a language called C+ before C++ (or C+ changed its name to C++, it is not clear what the guy is saying in the video). Is this correct? I never heard of just C+ before, is there any language called C+? If there is such language, is such language related to C++?
C+/ABCL is not related to C++ in any way, It is an different and unrelated language which is younger than C++.
I've never heard of it (and I've been around C++ for a long time now).
Bjarne Stroustrup doesn't mention it in The Design and Evolution of
C++_, and I guess he would know. The language was originally called
“C with Classes”, but the name was changed to
“C++” long before the language got out of AT&T. (There is
an ABCL/c+, but it has nothing to do with C++, and is much later.)
I have this classic question of how should the C++ Standard (I mean the actual official document of the finalized ones) e.g. C++98, C++03 be used to learn and teach C++. My idea is only from the point of view of an average C++ user and not from the point of view of the language lawyers or someone who wishes to be in the Standards committee, compiler writers and so on.
Here are my personal thoughts:
a) It is aweful place to start learning C++. Books like "C++ in a Nutshell", "The C++ programming Language" etc do a very good job on that front while closely aligning with the Standard.
b) One needs to revert to the Standard only when
a compiler gives a behavior which is not consistent with what the common books say or,
a certain behavior is inconsistent across compilers e.g. GCC, VS, Comeau etc. I understand the fact that these compilers could be inconsistent is in very few cases / dark corners of the language e.g. templates/exception handling etc. However one really comes to know about the possible different compiler behaviors only when either one is porting and/or migrating to a different environment or when there is a compiler upgrade e.g.
if a concept is poorly explained / not explained in the books at hand e.g. if it is a really advanced concept
Any thoughts/ideas/recommendation on this?
The C++ language standard would be an absolutely terrible place to start learning the language. It is dense, obtuse, and really long. Often the information you are looking for is spread across seven different clauses or hidden in a half of a sentence in a clause completely unrelated to where you think it should be (or worse, a behavior is specified in the sentence you ignored because you didn't think it was relevant).
It does have its uses, of course. To name a few,
If you think you've found a bug in a compiler, it's often necessary to refer to the standard to make sure you aren't just misunderstanding what the specified behavior is.
If you find behavior that is inconsistent between compilers, it's handy to be able to look up which is correct (or which is more correct), though often you'll need to write workarounds regardless.
If you want to know why things are the way they are, it is often a good reference: you can see how different features of the language are related and understand how they interact. Things aren't always clear, of course, but they often are. There are a lot of condensed examples and notes demonstrating and explaining the normative text.
If you reference the C++ standard in a post on Stack Overflow, you get more a lot more upvotes. :-)
It's very interesting to learn about the language. It's one thing to write code and stumble through getting things to compile and run. It's another thing altogether to go and try to understand the language as a whole and understand why you have to do things a certain way.
The standard should be used to ensure portability of code.
When writing basic c++ code you shouldn't need to refer to the standards, but when using templates or advanced use of the STL, reference to the standard is essential to maintain compatibility with more than one compiler, and forward compatibility with future versions.
I use g++ to compile my C++ programs and there I use the option -std=c++0x (earlier, -std=c++98) to make sure that my code is always standard compliant. If I get any warning or error regarding standard compliance, I research on that to educate myself and fix my code.
I mainly use C++ to do scientific computing, and lately I've been restricting myself to a very C-like subset of C++ features; namely, no classes/inheritance except complex and STL, templates only used for find/replace kinds of substitutions, and a few other things I can't put in words off the top of my head. I am wondering if there are any official or well-documented subsets of the C++ language that I could look at for reference (as well as rationale) when I go about picking and choosing which features to use.
There is Embedded C++. It sounds mostly similar to what you're looking for.
Google publishes its internal C++ style guide, which is often referred to as such a subset: https://google.github.io/styleguide/cppguide.html . Ben Maurer, whose company reCAPTCHA was acquired by Google, describes it as follows in this post on Quora:
You can basically think of Google's
C++ subset as C plus a bit of sugar:
The ability to add methods to structs
Basic single inheritance.
Collection and string classes
Scope based resource management.
They also publish a lint tool, cpplint.py.
Not long ago I listened to this SE-Radio podcast - Episode 152: MISRA with Johan Bezem, which introduces MISRA, standard guidelines for C and C++ to ensure better quality, try looking at it.
The GCC developers are about to allow some C++ features. I'm not aware of any official guidelines, yet, but I am pretty sure that they will define some. Have a look at initial report on the mailing list.
Well, latest developments (TR1, C++0x) in C++ made it very much generic, allowing you to do imperative, OOP or even (limited) functional programming in C++. Libraries like Boost also enable you to do very power declarative template-based meta-programming.
I think Boost is the first thing to try out in C++. It's a comprehensive library, which also includes several modules that enable you to program in functional style (Boost.Functional) or making compile-time declarative meta-programming (Boost MPL).
OpenCL has been using C for writing kernels, but they have recently added (or will soon add) C++ bindings and perhaps Java. OpenCL leaves out a number of performance robbing features of C. Excluded are things like function pointers and recursion. Smart pointers and polymorphism also create overhead.
Restrictions on C:
SIMD programming languages
Slightly off topic: Here is a good discussion comparing OpenCL with CUDA using C.
OpenCL or CUDA Which way to go?
The SEI CERT C++ Coding Standard gives a list of rules for writing safe, reliable, and secure systems in C++14. This is not a subset of C++ per se, but as a coding standard like the other answers is a subset in effect by avoiding unsafe, undefined, or easily-misused features (including some common to C).