[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
C++ support (2)
From: |
Bruno Haible |
Subject: |
C++ support (2) |
Date: |
Tue, 31 Oct 2006 20:20:35 +0100 |
User-agent: |
KMail/1.9.1 |
Here is a proposed patch to make assignments of the form
buffer = xrealloc (buffer, new_bufsize);
compile without errors or warnings in C++ mode, without need to add a cast.
The trick for doing this is to notice that the expected type of the result
is the type of the first argument.
Unfortunately, for xmalloc, such a thing is not possible.
OK to apply?
2006-10-29 Bruno Haible <address@hidden>
* lib/xalloc.h (xrealloc, xnrealloc, x2realloc, x2nrealloc, xmemdup)
[C++]: Add function template that provides result type propagation.
*** gnulib-20061026/lib/xalloc.h 2005-05-14 08:03:58.000000000 +0200
--- gnulib-20061026-modified/lib/xalloc.h 2006-10-30 02:42:28.000000000
+0100
***************
*** 49,59 ****
--- 49,110 ----
void *xnmalloc (size_t n, size_t s);
void *xzalloc (size_t s);
void *xcalloc (size_t n, size_t s);
+
void *xrealloc (void *p, size_t s);
+ #ifdef __cplusplus
+ }
+ template <typename T>
+ inline T * xrealloc (T * ptr, size_t s)
+ {
+ return (T *) xrealloc ((void *) ptr, s);
+ }
+ extern "C" {
+ #endif
+
void *xnrealloc (void *p, size_t n, size_t s);
+ #ifdef __cplusplus
+ }
+ template <typename T>
+ inline T * xnrealloc (T * ptr, size_t n, size_t s)
+ {
+ return (T *) xnrealloc ((void *) ptr, n, s);
+ }
+ extern "C" {
+ #endif
+
void *x2realloc (void *p, size_t *pn);
+ #ifdef __cplusplus
+ }
+ template <typename T>
+ inline T * x2realloc (T * ptr, size_t *pn)
+ {
+ return (T *) x2realloc ((void *) ptr, pn);
+ }
+ extern "C" {
+ #endif
+
void *x2nrealloc (void *p, size_t *pn, size_t s);
+ #ifdef __cplusplus
+ }
+ template <typename T>
+ inline T * x2nrealloc (T * ptr, size_t *pn, size_t s)
+ {
+ return (T *) x2nrealloc ((void *) ptr, pn, s);
+ }
+ extern "C" {
+ #endif
+
void *xmemdup (void const *p, size_t s);
+ #ifdef __cplusplus
+ }
+ template <typename T>
+ inline T * xmemdup (const T * ptr, size_t s)
+ {
+ return (T *) xmemdup ((const void *) ptr, s);
+ }
+ extern "C" {
+ #endif
+
char *xstrdup (char const *str);
/* Return 1 if an array of N objects, each of size S, cannot exist due
- C++ support (2),
Bruno Haible <=