Passing string literals in C++

Hello,

I've recently bumped into lots of annoying messages about deprecated
conversion from string constant to 'char*' when I was using libunbound
with C++.

Apparently, functions in <unbound.h> can safely handle const char*
parameters, but they are declared and defined to take char*. This
represents no problem in C. But in C++ string literals are considered
const char*.

If there is no reason for declaring those functions with char* arguments
I would suggest to change the function prototypes to take const char*
arguments. The suggested patch is in the attachment.

Best regards,
K.

(attachments)

unbound_const_strings.diff (8.71 KB)

Hi Karel,

Thanks for the patch. I wanted the initial header file to use the
least language constructs as these give rise to portability and
parsing issues (such as types, 'pid_t'). This is why all the numbers
are passed as 'int'. const seems to be 'C99' and posix :slight_smile:

Best regards,
   Wouter

Hi Wouter,

could you just check the availability of __cplusplus >= 199711L and or C99 support (either by using macro or from autoconf) and use something like:

#ifdef C99_or_CXX11_pseudo_macro
#define UB_CONST const
#else
#define UB_CONST
#endif

and use:

int ub_function(UB_CONST char *);

?

Hi Ondrej,

This sort of construct is very annoying to use, because a system
header file may be used by different compilers (e.g. clang, gcc,
suncc, icc ..) and different programs may use a different standards-level.

So, right now, I have committed 'const' and hope everyone supports it.

Best regards,
   Wouter