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
This commit is contained in:
Olaf Bergmann committed 2018-07-27 16:23:57 +02:00
1 parent ccb01d0257
commit 15a45f6f24
1 file changed
+11 -1
+11 -1
View File
@@ -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<const uint8_t *>(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