Dictionaries, Restricted templates, Decorators and Events in C++

Hi all,

This is intended to be a discussion rather than a question; all opinions are welcome, and there is probably no best answer.

C++ is an efficient language. It's syntax is arguably simple, it's grammar especially rich and complex, and I would say that more than with other languages, writing beautiful code in C++ sadly requires a lot (a lot) of discipline, knowledge, and reflexion.

I have a very modest experience in programming, but I had the chance to play around with many different languages, each coming with it's advantages and drawbacks. C++0x comes with a lot of useful improvements (my top 3 is: smart pointers, random and regex). Still, some things keep annoying me from time to time when I have to write in C++; I am curious if I am the only one and if some of you would have solutions to share.

About dictionaries
This subject is very documented on the web.
The need is simple: How to efficiently index (linearly or not) objects of DIFFERENT types in C++?
How come there is no solution for such thing in the STL (without union or void*..)? Is the "mess-potential" too high?

About templates
Templates are very handy and save a lot of code duplication, sometimes.
However, one thing I would find natural would be a way of restricting the values that the template can take, so as to avoid mystical errors when used with a type that was not thought of at implementation time. There is a way (if you call explicit instantiation a way...) of doing something like that when exporting the compiled code in a DLL, but that's just an ad hoc trick to me.

About decorators
Those of you who know Python know what I am talking about (http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/). They are an easy and clean way of encapsulating a function in another one. I would really need this in C++, mostly for debugging purposes and event-based programming. Is there any way to decorate functions?

About event-based programming
Is there an easy, robust, lightweight, cross-platform way to program with events in C++ (create, trigger, listen, callback... or signal/slot like Qt)?

Thanks in advance, I'm really curious to read your answers!
John
Last edited on
May be this would be better in Lounge.

About dictionaries
There might be also variant in boost. What do you want more?

About templates
I think, C++ lacks a way to tell the user of a template (class) what exactly is done with the template parameter (like operator= or operator<). I wouldn't find restriction very usefull in this context.

About event-based programming
Look at boost:
http://www.boost.org/doc/libs/1_50_0/libs/functional/
http://www.boost.org/doc/libs/1_50_0/libs/bind/bind.html
How to efficiently index (linearly or not) objects of DIFFERENT types in C++ [...] (without union or void*..)

C++ is statically typed: to construct an accessor function type that converts an index type to value type if the values are "DIFFERENT" (I assume, can't be represented as a common base type), you need either
a) type erasure (full: void* / boost.any, partial: std::function, or discriminated: union / boost.variant).
b) complete information about the type of each element of the sequence (std::tuple, boost.fusion)

restricting the values that the template can take

The standard tool to restrict the types the template can be instantiated with is (boost|std) enable_if: http://en.cppreference.com/w/cpp/types/enable_if

an easy and clean way of encapsulating a function in another one.

This is nothing special: just take an function argument (a template parameter, std::function, or something more specific) and call it.

signal/slot like Qt)?

boost has signals and slots too.
Topic archived. No new replies allowed.