user98188
user98188

Reputation:

What are some of the more obscure parts of C++?

I've read quite a few beginner's books on C++, and a little beyond that, but what are some of the more obscure aspects of C++, or where can I find information/tutorials on these?

Upvotes: 11

Views: 4302

Answers (11)

Helixirr
Helixirr

Reputation: 931

Some obscure C++ features:

Upvotes: 6

jcoffland
jcoffland

Reputation: 5431

No one mentioned what I think it one of the weirdest parts of C++, obviously an after thought, the syntax for the pre-decrement and pre-increment operators.

class A {
public:
  A &operator++() {...} // Post increment
  A &operator--() {...} // Post decrement
};

vs.

class A {
public:
  A &operator++(int) {...} // Pre increment
  A &operator--(int) {...} // Pre decrement
};

Of course post increment means add one but return the previous value and pre increment means add one and return the new value. E.g.:

A a;
f(a++); // Post increment a
f(++a); // Pre increment a

I'm not sure what happens if you try to use or pass the 'int' argument. You could try to pass it like this:

obj.operator++(0);

I suppose that is how you differentiate between the two operators when calling them explicitly.

Upvotes: 1

Lance Diduck
Lance Diduck

Reputation: 1561

Very obscure:

  • void operator,();
  • precedence of operator||() and operator&&() when short circuiting
    logical expressions
  • what happens when you overload operator&()
  • order of initialization of stateful virtual inhertiance.
  • deferencing pointers to template members
  • function try blocks
  • the allocator::rebind syntax
  • non default behaviour of std::unexpected
  • std::cout.imbue()
  • Anything with locales,especially custom facets
  • overriding via dominance
  • trigraphs

Just to name a few

Upvotes: 2

Dan Olson
Dan Olson

Reputation: 23367

ADL (aka Koenig Lookup) is pretty obscure, even though people use it without realizing it in every Hello World program.

The "ScopeGuard trick", where const references as return values from functions are bound to the scope of something they're assigned to, is also fairly obscure. That article raised awareness about it quite a bit, though.

There are also a few properties and usages of sizeof() that count as obscure, especially when used in macros or template metaprograms.

Upvotes: 9

Matej
Matej

Reputation: 6199

If you want to learn something obscure about C++, try templates in depth and read Modern C++ Design by Andrei Alexandrescu. This book is classics about template metaprogramming.

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264381

Not so obvious is the implementation of certain methods in terms of other methods.

Assignment operator:
-> implement as a copy construction and swap.

operator +
-> implemented as copy construction and operator +=

operator !=
-> implement in terms of operator ==

etc...

The concept is that the work should be localized in one method and the other methods use this method to do the real work then add their unique twist to it.

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264381

All books explain what exceptions are.
But very few talk about exception safety and the exception grantees.

How to use RAII (easy example smart pointers) to make your code exception safe.

What are the exception guarantees that you should provide.

  • Destructor should grantee that no exceptions escape.
  • Several of the STL algorithms use things like swap which also should grantee no exceptions escape to work correctly in all situations.
  • etc...

Upvotes: 1

FeatureCreep
FeatureCreep

Reputation: 1824

Actually caring about bad_alloc.

no?

Edit: what I mean is that in many of the sometimes huge C++ projects in which I have had the pleasure to fix bugs, the concept of catching bad_alloc and acting upon it have been missing. That would put it in the "obscure" part of C++, even though it should not be.

Upvotes: 4

Dario
Dario

Reputation: 49218

Template metaprogramming (an entire litte turing-complete programming language in C++ executed by the compiler) and hacks with the preprocessor can be very hard! (You can even create completely new syntax with this - just take a look at boost::lambda)

But I think the most important thing to learn and understand is the STL (C++ standard library) which is inevitably useful but may look somewhat strange.

Upvotes: 1

Thomas L Holaday
Thomas L Holaday

Reputation: 13814

The aphorism for the candidate features is "you don't need it very often, but when you need it, you need it bad."

  • Placement new
  • extern "C++"
  • local classes
  • std::allocator
  • mutable, explicit, volatile
  • pointer-to-any-member-of-any-class

So, for the people who have had reason to use these features (library authors), they won't be obscure, and for the majority of C++ programmers, they will be unknown.

Upvotes: 6

Alex Martelli
Alex Martelli

Reputation: 881595

Herb Sutter's books are an excellent source for this topic -- start with http://www.gotw.ca/publications/xc++.htm .

Upvotes: 2

Related Questions