bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gnulib and C++


From: Bruno Haible
Subject: Re: gnulib and C++
Date: Sat, 20 Feb 2010 03:02:14 +0100
User-agent: KMail/1.9.9

John W. Eaton wrote:
> If I add "using gnulib::open;" inside the "#ifdef __cplusplus" block,
> ...
> then I don't even have to prefix all of the uses of open in my code
> with gnulib::.  This seems like the best solution for me.

A good idea. Unfortunately I cannot see how to make it work: This program

================================ foo.cc ===============================
#include <fcntl.h>

extern "C" {
extern int rpl_open (const char *filename, int flags, ...) __attribute__ 
((__nonnull__ (1)));
}

namespace gnulib
{
  int (*const open) (const char *filename, int flags, ...) = rpl_open;
}

class foo {
  int open (const char *arg);
};

int bar_uses_global () {
  return open ("/dev/null", O_RDWR);
}

// Does not work, leads to
//   error: reference to ‘open’ is ambiguous
//   /usr/include/fcntl.h:85: error: candidates are: int open(const char*, int, 
...)
//   foo.cc:9: error:                 int (* const gnulib::open)(const char*, 
int, ...)
//using namespace gnulib;

int bar_uses_gnulib () {
  return gnulib::open ("/dev/null", O_RDWR);
}

// John Eaton suggests this.
using gnulib::open;

int bar_uses_gnulib_implicitly () {
  return open ("/dev/null", O_RDWR);
}

// Compatibility with future C++1X:

namespace posix = gnulib;

int bar_uses_gnulib_via_posix () {
  return posix::open ("/dev/null", O_RDWR);
}
=======================================================================

gives an error (on a glibc system with gcc 4.3.1):

$ g++ -S foo.cc
foo.cc:31: error: ‘open’ is already declared in this scope

And when I change the first line to

namespace system {
#include <fcntl.h>
}
using namespace system;

then there is a different error message:

$ g++ -S foo.cc
foo.cc: In function ‘int bar_uses_gnulib_implicitly()’:
foo.cc:37: error: reference to ‘open’ is ambiguous
foo.cc:12: error: candidates are: int (* const gnulib::open)(const char*, int, 
...)
/usr/include/fcntl.h:85: error:                 int system::open(const char*, 
int, ...)
foo.cc:37: error: reference to ‘open’ is ambiguous
foo.cc:12: error: candidates are: int (* const gnulib::open)(const char*, int, 
...)
/usr/include/fcntl.h:85: error:                 int system::open(const char*, 
int, ...)

Do you see a way to make it work?

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]