From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id D77514041CF for ; Mon, 9 Apr 2012 14:31:42 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id p27yqgte60DQ for ; Mon, 9 Apr 2012 14:31:42 -0700 (PDT) Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id D49B84041C4 for ; Mon, 9 Apr 2012 14:31:41 -0700 (PDT) Received: by lahc1 with SMTP id c1so3966278lah.26 for ; Mon, 09 Apr 2012 14:31:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:in-reply-to:references:date:message-id:mime-version :content-type:x-gm-message-state; bh=AY7koQTds8NWhz9uJK02k6P8bSg/aP55lxA8gPkTKe4=; b=PfMHwIrWncbgpqnmsXZ9pzSFQevk2TuucYF6lBvbFxcjg4bijeQ6uf3ow9i8/3MBOD iKhIGRQ97UqYrts56RqWAPSAzpaHTwSmlw8QthtwpeEiCkRKR0mrOGBL4nLeYjMKBG2S q/vtLdVyPq0AHSqqXcQA72OcofHdclLonbOa1JtoyLsvi+qUO7f/k37lDsaJiHCdkbYm TFazyXgxFdKqrN869Yiw+3blnH7eBHzKEEr4ORhuU0/ZaLu1JDGpSTprluFirfbT0i8T f7de+w6qfdmoty/y0ddw34qUwj4xTQn64lRL5jIOJji3XSRq80NaXVb3eRfxJceGMlwC Bujg== Received: by 10.152.125.41 with SMTP id mn9mr13092580lab.30.1334007098775; Mon, 09 Apr 2012 14:31:38 -0700 (PDT) Received: from localhost (dsl-hkibrasgw4-fe50dc00-68.dhcp.inet.fi. [80.220.80.68]) by mx.google.com with ESMTPS id u4sm17074007lad.5.2012.04.09.14.31.36 (version=SSLv3 cipher=OTHER); Mon, 09 Apr 2012 14:31:37 -0700 (PDT) From: Jani Nikula To: Vladimir Marek , Notmuch Mail Subject: Re: [PATCH 4/4] Explicitly type void* pointers In-Reply-To: <20120409181543.GC10554@pub.czech.sun.com> References: <1333966665-10469-1-git-send-email-Vladimir.Marek@oracle.com> <1333966665-10469-5-git-send-email-Vladimir.Marek@oracle.com> <20120409181543.GC10554@pub.czech.sun.com>User-Agent: Notmuch/0.12+81~g839a805 (http://notmuchmail.org) Emacs/23.3.1 (i686-pc-linux-gnu) Date: Tue, 10 Apr 2012 00:31:34 +0300 Message-ID: <87mx6ka8y1.fsf@nikula.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gm-Message-State: ALoCoQnwprzR8WVSMLExIIU3AmRHsiK8Zh3/rUo+S+L15VPsqqP3Qjz2vaOOwAdsYVGbT5VgzRV7 X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 Apr 2012 21:31:43 -0000 Vladimir Marek writes: > Hi, > >> Hi, does notmuch not compile without this? IIRC talloc_steal is a macro >> that's supposed to provide type safety (at least with GCC), and I'd be >> hesitant about adding the casts. Please look in your talloc.h. > > It does not compile. It might be that I'm using Sun/Oracle CC instead of > gcc. The error looks like this: > > "lib/database.cc", line 1368: Error: Cannot assign void* to const char*. In general, that's not a difference in the C++ compilers. You can't assign 'void *' to 'T *' in C++. > When looking into talloc documentation, the definition seems to be: > > void* talloc_steal ( const void * new_ctx, const void * ptr ) > > http://talloc.samba.org/talloc/doc/html/group__talloc.html#gaccc66139273e727183fb5bdda11ef82c > > > When looking into talloc.h, it says: > > /* try to make talloc_set_destructor() and talloc_steal() type safe, > if we have a recent gcc */ It just so happens that the trick for type safety fixes the problem for recent GCC by having an explicit cast. > So, maybe the way to satisfy everyone would be: > > notmuch_message_t *tmp_message = message; > talloc_steal(notmuch, tmp_message); > return(tmp_message); > > Or alternatively, > > #if (__GNUC__ >= 3) > return talloc_steal (notmuch, message); > #else > return (notmuch_message_t*) talloc_steal (notmuch, message); > #fi > > > Of course I'm happy either way :) I'm throwing in a third alternative below. Does it work for you? I think it's both prettier and uglier than the above at the same time! ;) A middle ground would be to change the callers to use "notmuch_talloc_steal", and just #define notmuch_talloc_steal talloc_steal if __GNUC__ >= 3. One could argue upstream talloc should have this, but OTOH it's a C library. BR, Jani. diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index ea836f7..83b46e8 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -499,4 +499,22 @@ _notmuch_filenames_create (const void *ctx, NOTMUCH_END_DECLS +#ifdef __cplusplus +/* Implicit typecast from 'void *' to 'T *' is okay in C, but not in + * C++. In talloc_steal, an explicit cast is provided for type safety + * in some GCC versions. Otherwise, a cast is required. Provide a + * template function for this to maintain type safety, and redefine + * talloc_steal to use it. + */ +#if !(__GNUC__ >= 3) +template +T *notmuch_talloc_steal(const void *new_ctx, const T *ptr) +{ + return static_cast(talloc_steal(new_ctx, ptr)); +} +#undef talloc_steal +#define talloc_steal notmuch_talloc_steal +#endif +#endif + #endif