cplusplus.co.il

Odd man out

Posted by: rmn on: 24/04/2010

There’s nothing like the “Eureka” moment when you eventually manage to solve a challenging puzzle. I’m a man of puzzles myself, and the ones I like the most are computer science, or programming related, puzzles.

I’ve recently heard a pretty intriguing puzzle I would like to share with you.

Read the rest of this entry »

Quines

Posted by: rmn on: 16/04/2010

A Quine is a computer program which prints a copy of its own source code as its only output.

Thus it is theoretically possible to compile such a program, run it, and then have its output compiled again to produce the initial program – in an infinite loop, forever.

Read the rest of this entry »

Catching uncaught exceptions within terminate

Posted by: rmn on: 21/03/2010

The handler std::terminate() is called whenever the exception handling mechanism cannot find a suitable catch clause for a thrown exception (and in some other cases. For example, when an exception is thrown during the handling of another exception – see this GotW post about std::uncaught_exception). It is possible to define a custom handler by using std::set_terminate.

In this post we would like to create a terminate handler which will be able to catch the exception that led to its invocation, when there is one.

Read the rest of this entry »

Escaping overloaded operators

Posted by: rmn on: 19/02/2010

The possibility of overloading just about any C++ operator and having it do something entirely different from what it was designed for, can sometimes make life pretty hard.

Here are a couple of examples: What if you wanted to take the address of an object, which had implemented an entirely different semantic for the ampersand (&) operator? Or what if somebody decided to overload the comma operator in some strange manner?

As you could have guessed, there are solutions for such scenarios.

Read the rest of this entry »

Metalists for fun and profit

Posted by: nadavrot on: 07/02/2010

My name is Nadav Rotem and I am a guest blogger on this blog. I am here to write about metalists. Not metalists like Metallica or Iron Maiden, but meta-lists. Lists which are “template maiden”.

The First thing I am going to show you is how to create a data structure which is similar to a linked list. Next we are going to define the Push, Pop and Concat operations. After we have those, we will implement the Walsh transform (similar to FFT on integers) on our list. After that, we will use template templates to define generators to create lists. Finally we are going to implement the Map and Reduce operations on our lists. Let’s start.

Read the rest of this entry »

Compile time primality test

Posted by: rmn on: 27/01/2010

The powerful template mechanism of C++ allows us to write pretty complex Meta Functions, which are executed by the compiler during compilation. There are two basic types of meta-functions: one whose result is a type (mainly dealt with by Boost.MPL), and the other is a compile-time computation (which can result in any compile time constant). In this post we will review an example of the latter.

We would like to achieve a compile time boolean constant of the form is_prime<any constant number>::res. The problem with such meta programming task, in my opinion, is that it requires a functional programming mindset. Something that we, C++ programmers, aren’t necessarily used to. But I am sure we will be able to tackle it anyway.

Read the rest of this entry »

A question of memory layout

Posted by: rmn on: 20/01/2010

Actual object memory layout can be a little tricky when inheritance and its virtual tables are involved. And it gets even trickier when pointer arithmetic is employed. Do you consider yourself a low-level expert?

Read the rest of this entry »

Follow

Get every new post delivered to your Inbox.

Join 25 other followers