From boris at kolpackov.net Thu Mar 11 15:21:51 2004 From: boris at kolpackov.net (Boris Kolpackov) Date: Thu Mar 11 15:20:06 2004 Subject: c++: notes on namespace In-Reply-To: <20040223222837.GA17775@kolpackov.net> References: <20040223222837.GA17775@kolpackov.net> Message-ID: <20040311212151.GA3957@kolpackov.net> Good day, Some additional notes/corrections: > @@ Using-directive is an efficient way to 'inherit' namespaces. > > > @@ Using-directive allow you to 'override' some declarations > (see also DEC++ 17.4.3 last paragraph; PLC++ 8.2.8.2). The latter using-directive should be using-declaration. As of composing namespaces out of other namespaces I found an interesting GNU extension called "strong using": http://gcc.gnu.org/onlinedocs/gcc/Strong-Using.html#Strong%20Using In particular it shows two potential problems in standard using-directive with regards to the namespace composition technique. hth, -boris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 652 bytes Desc: Digital signature Url : http://www.kolpackov.net/pipermail/notes/attachments/20040311/045af055/attachment.bin From boris at kolpackov.net Mon Mar 29 13:12:32 2004 From: boris at kolpackov.net (Boris Kolpackov) Date: Mon Mar 29 13:10:40 2004 Subject: biggest c++ contradiction Message-ID: <20040329191232.GA10866@kolpackov.net> Good day, Recently I've been reading a lengthy thread where people were naming their favorite uglinesses of c++. Of course, multiple inheritance popped up not once. As I was reading along I started wondering if anyone would name something that I could agree with. Surely nobody did and I had to think one out myself. So, IMO, the biggest c++ contradiction that appears in every c++ book you would care to name is the suggestion to use RAII and not to throw from destructors. have a nice day, -boris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 652 bytes Desc: Digital signature Url : http://www.kolpackov.net/pipermail/notes/attachments/20040329/46c26e18/attachment-0001.bin From boris at kolpackov.net Wed Mar 31 17:15:50 2004 From: boris at kolpackov.net (Boris Kolpackov) Date: Wed Mar 31 17:20:42 2004 Subject: gcc: __attribute__ ((weak)) Message-ID: <20040331231550.GA11835@kolpackov.net> Good day, While playing with glibc I noticed one interesting technique. It is based on gcc's extension that allows you to declare weak symbols. In essence it works like this: extern void weak_f (void) __attribute__ ((weak)); int main () { if (weak_f) { weak_f (); } } When you link such code ld won't complain if weak_f is unresolved. Plus you can check at run-time if symbol has been resolved as shown above. Interesting effects can be achieved using this technique. For example you can make some code thread-safe on-demand. You compile this code once and depending on kind of application it is used in it automatically becomes multi-threaded or single-threaded: #include extern "C" int pthread_create (pthread_t*, const pthread_attr_t*, void* (*)(void*), void*) __attribute__ ((weak)); extern "C" int pthread_mutex_init (pthread_mutex_t*, const pthread_mutexattr_t*) __attribute__ ((weak)); extern "C" int pthread_mutex_lock (pthread_mutex_t*) __attribute__ ((weak)); extern "C" int pthread_mutex_unlock (pthread_mutex_t*) __attribute__ ((weak)); extern "C" int pthread_mutex_destroy (pthread_mutex_t*) __attribute__ ((weak)); class foo { public: foo () { if (pthread_create) pthread_mutex_init (&m_, 0); } ~foo () { if (pthread_create) pthread_mutex_destroy (&m_); } bar () { if (pthread_create) pthread_mutex_lock (&m_); // ... if (pthread_create) pthread_mutex_unlock (&m_); } private: pthread_mutex_t m_; // some other data that mutex protects }; Now if you link your code with libpthread, foo is automatically multi-thread-safe. If you don't then all the calls to mutex API are skipped. This could be especially nice if class foo is in the library that is used by both kinds of applications. -boris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 652 bytes Desc: Digital signature Url : http://www.kolpackov.net/pipermail/notes/attachments/20040331/da607ad4/attachment.bin