From: Bernd Petrovitsch <bernd@sysprog.at>
To: Josh Triplett <josh@joshtriplett.org>
Cc: Tomas Klacko <tomas.klacko@gmail.com>,
Christopher Li <sparse@chrisli.org>,
linux-sparse@vger.kernel.org
Subject: Re: including sparse headers in C++ code
Date: Tue, 12 Oct 2010 12:42:31 +0200 [thread overview]
Message-ID: <1286880151.24953.50.camel@thorin> (raw)
In-Reply-To: <20101011233706.GA10991@feather>
On Mon, 2010-10-11 at 16:37 -0700, Josh Triplett wrote:
[....]
> Wow. I had to double-check this because I couldn't quite believe C++
> had that degree of dain bramage, but sure enough:
>
> /tmp$ cat test.c
> extern void *pv(void);
>
> int *pi(void)
> {
> return pv();
> }
> /tmp$ gcc -c test.c -o /dev/null
> /tmp$ g++ -c test.c -o /dev/null
> test.c: In function ‘int* pi()’:
> test.c:5: error: invalid conversion from ‘void*’ to ‘int*’
> (1) /tmp$
Welcome to one of the (more obvious) differences between C and C++: you
can't assign/convert/promote a "void *" to any other pointer type (and
vice versa) without explicit type-cast.
For a library written in C, that is the point where "compile it also as
C++" ends IMHO. Just keep the 'extern "C" {' stuff in headers (under
'#ifdef __cplusplus') and compile it as C (and not C++).
BTW there are other similar issues if one digs deep enough.
BTDT (read: looked after a library which was used in 2 "applications" -
one in pure C, the other C++. No way without explicit type-casts which
look somewhat funny and are surely superfluous in C as such).
> I can understand C++ having stronger typechecking, but void pointers
> *exist* for this purpose. *Really* debatable whether Sparse should work
> around this. Avoiding keywords, sure, but casting void pointers
> everywhere? People *remove* these kinds of casts from C programs as a
> cleanup.
Type-casts are inherently evil. But in several situations there is in C
no other (or better) workaround. The above - kill warnings through "void
*" type-casts - makes it worse because some people (including /me) use
the type system in C and want the compiler to check as much as possible.
Bernd
--
mobile: +43 664 4416156 http://www.sysprog.at/
Linux Software Development, Consulting and Services
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
prev parent reply other threads:[~2010-10-12 10:42 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-09 16:40 including sparse headers in C++ code Tomas Klacko
2010-10-09 20:59 ` Josh Triplett
2010-10-09 21:46 ` Christopher Li
2010-10-10 11:41 ` Bernd Petrovitsch
2010-10-10 11:52 ` Kamil Dudka
2010-10-11 9:44 ` Bernd Petrovitsch
2010-10-11 16:04 ` Christopher Li
2010-10-11 19:12 ` Josh Triplett
2010-10-13 14:45 ` Bernd Petrovitsch
2010-10-18 18:43 ` Christopher Li
2010-10-20 7:29 ` Al Viro
2010-10-20 9:39 ` Bernd Petrovitsch
2010-10-20 15:34 ` Christopher Li
2010-10-29 13:22 ` Bernd Petrovitsch
2010-11-05 0:57 ` Christopher Li
2010-11-09 13:28 ` Bernd Petrovitsch
2010-11-09 22:52 ` Christopher Li
2010-11-10 10:52 ` Bernd Petrovitsch
2010-10-11 22:33 ` Tomas Klacko
2010-10-11 22:46 ` Al Viro
2010-10-11 23:01 ` Christopher Li
2010-10-12 22:45 ` Tomas Klacko
2010-10-13 0:37 ` Christopher Li
2010-10-13 11:39 ` Bernd Petrovitsch
2010-10-16 16:03 ` Tomas Klacko
2010-10-16 19:11 ` Josh Triplett
2010-10-17 10:31 ` Tomas Klacko
2010-10-18 4:13 ` Christopher Li
2010-10-18 5:39 ` Josh Triplett
2010-10-18 18:37 ` Christopher Li
2010-10-19 20:03 ` Tomas Klacko
2010-10-19 21:31 ` Al Viro
2010-10-19 21:46 ` David Malcolm
2010-10-19 22:12 ` Al Viro
2010-10-19 22:49 ` Tomas Klacko
2010-10-20 10:19 ` Bernd Petrovitsch
2010-10-19 23:07 ` Christopher Li
2010-10-20 7:40 ` Al Viro
2010-10-18 3:16 ` Christopher Li
2010-10-11 23:37 ` Josh Triplett
2010-10-12 10:42 ` Bernd Petrovitsch [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1286880151.24953.50.camel@thorin \
--to=bernd@sysprog.at \
--cc=josh@joshtriplett.org \
--cc=linux-sparse@vger.kernel.org \
--cc=sparse@chrisli.org \
--cc=tomas.klacko@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).