From 15a45f6f245815d3b3ee19fbfc63171a3631296f Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 27 Jul 2018 16:23:57 +0200 Subject: [PATCH] str.h: fix coap_make_str_const macro for C++ As C++ forbides returning a pointer to a temporary object, the creation of a coap_str_const_t to wrap the character-array and size done in coap_make_str_const() does not work. This change therefore creates a C++ class CoAPStrConst in namespace libcoap in a way that allows the default constructor to be used with aggregate initialization. This commit closes issue #233 [2]. [1] https://en.cppreference.com/w/cpp/language/aggregate_initialization [2] https://github.com/obgm/libcoap/issues/233 --- include/coap/str.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/coap/str.h b/include/coap/str.h index 72c1eaa1..2cd21543 100644 --- a/include/coap/str.h +++ b/include/coap/str.h @@ -96,8 +96,18 @@ void coap_delete_str_const(coap_str_const_t *string); * * @param string The const byte array to convert to a coap_str_const_t * */ -#define coap_make_str_const(string) \ +#ifdef __cplusplus +namespace libcoap { + struct CoAPStrConst : coap_str_const_t { + operator coap_str_const_t *() { return this; } + }; +} +#define coap_make_str_const(CStr) \ + libcoap::CoAPStrConst{sizeof(CStr)-1, reinterpret_cast(CStr)} +#else /* __cplusplus */ +#define coap_make_str_const(string) \ (&(coap_str_const_t){sizeof(string)-1,(const uint8_t *)(string)}) +#endif /* __cplusplus */ /** * Compares the two strings for equality