Re: [PATCH kvm-unit-tests 2/4] Introduce a C++ wrapper for the kvm APIs

Avi Kivity
2010-11-24T17:35:46Z

On 11/24/2010 07:27 PM, Anthony Liguori wrote:
>
> The alternative in C is:
>
> struct file_operations {
> void (*release)(void);
> void (*read)(...);
> void (*write)(...);
> };
>
> struct file {
> struct file_operations *ops;
> };
>
> If I do:
>
> static file_operations posix_file_ops = {
> .read = posix_file_read,
> .write = posix_file_write,
> };
>
> struct posix_file {
> struct file parent;
> int fd;
> };
>
> void posix_file_init(struct posix_file *obj)
> {
> obj->parent.ops = posix_file_ops;
> obj->fd = ...;
> };
>
> The compiler won't generate an error. Only upon a call to
> file_release() will a null pointer dereference happens whereas in C++,
> because this paradigm is structured in the language, the compiler can
> help assist you.

You forgot to mention:

static void posix_file_read(struct file *_file, ...)
{
struct posix_file *file = container_of(_file, struct posix_file,
parent);

...
}

Even more boilerplate and potential for error. But I think the worst
thing about this is that it makes it hard to do the right thing, so
people hack quick and dirty workarounds. C++ makes this pattern trivial
(if : laden), so people are encouraged to use it.

--
error compiling committee.c: too many arguments to function