Re: Compounds in Init lists
On Jul 3, 9:13 am, gwowen <gwo...@gmail.com> wrote:
I wrote a simple wrapper class around pthreads to implement a
producer/consumer thread pool. A simplified form resembled
this:
#include <pthread.h>
class C
{
pthread_cond_t m_cond;
}
// with constructor
C::C ()
: m_cond(PTHREAD_COND_INTIALIZER)
{}
This isn't really legal.
On win32 g++/mingw, this compiled beautifully, but on Linux,
it didn't. This is because the typedefs differ.
On Linux, pthread_cond_t is a struct, and the macro
PTHREAD_COND_T is (something like) {0,0,NULL,0}, which can't
be used in an initialization list.
On most systems I know, pthread_cond_t will be a struct.
On Win32 pthread_cond_t is a pointer to an incomplete type.
Local coding standards say I should initialize the compound in
the initialization list.
Formally, you can't initialize a pthread_cond_t in an
initialization list. Regardless of what local coding standards
say. This is often an issue when interfacing with C or with
APIs defined in terms of C. The API uses struct's, and you
can't initialize a struct in an initialization list. In Posix,
unless the specification says explicitly that the type is not a
struct, you must suppose that it might be a struct. And some
types are required to be structs: how would you initialize a
struct tm or a struct stat in an initialization list?
Is this possible without doing, say the following:
namespace {
static const pthread_cond_t
pthread_cond_intializer = PTHREAD_COND_INTIALIZER;
}
C::C ()
: m_cond(pthread_cond_intializer)
{}
That will almost certainly get the code through the compiler,
and in practice, I can't imagine it not working. But it's not
legal Posix---you're not allowed to copy pthread_cont_t.
Will it be possible in C++0x?
I don't think so.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34