From ad37651726f5a12a387fbd1fc626a16c2cd62174 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 22 Feb 2021 16:54:47 -0500 Subject: [PATCH] Guarding undefined templates with a static_assert. (#1454) * Guarding undefined templates with a static_assert. * Fixing comments. * Undeprecating (or whatever you want to call it). --- include/simdjson/dom/element.h | 17 +++++++-------- include/simdjson/generic/ondemand/document.h | 23 +++++++++++--------- include/simdjson/generic/ondemand/value.h | 9 +++++++- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/simdjson/dom/element.h b/include/simdjson/dom/element.h index eb33befe..49ae8618 100644 --- a/include/simdjson/dom/element.h +++ b/include/simdjson/dom/element.h @@ -198,14 +198,6 @@ public: simdjson_really_inline bool is() const noexcept; /** - * @private - * Deprecated as a public interface. These methods will be made private in a future - * release. Use get_double(), get_bool(), get_uint64(), get_int64(), - * get_object(), get_array() or get_string() instead. We found in practice that - * the template would mislead users into writing get() for types X that - * are not among the supported types (e.g., get(), get(), - * get()), and the resulting C++ compiler error is difficult to parse. - * * Get the value as the provided type (T). * * Supported types: @@ -215,6 +207,9 @@ public: * - Array: dom::array * - Object: dom::object * + * You may use get_double(), get_bool(), get_uint64(), get_int64(), + * get_object(), get_array() or get_string() instead. + * * @tparam T bool, double, uint64_t, int64_t, std::string_view, const char *, dom::array, dom::object * * @returns The value cast to the given type, or: @@ -222,7 +217,11 @@ public: */ template - inline simdjson_result get() const noexcept; + inline simdjson_result get() const noexcept { + // Unless the simdjson library provides an inline implementation, calling this method should + // immediately fail. + static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library."); + } /** * Get the value as the provided type (T). diff --git a/include/simdjson/generic/ondemand/document.h b/include/simdjson/generic/ondemand/document.h index c86ee734..908ad0b6 100644 --- a/include/simdjson/generic/ondemand/document.h +++ b/include/simdjson/generic/ondemand/document.h @@ -100,24 +100,27 @@ public: simdjson_really_inline bool is_null() noexcept; /** - * @private - * Deprecated as a public interface. These methods will be made private in a future - * release. Use get_double(), get_bool(), get_uint64(), get_int64(), - * get_object(), get_array() or get_string() instead. We found in practice that - * the template would mislead users into writing get() for types X that - * are not among the supported types (e.g., get(), get(), - * get()), and the resulting C++ compiler error is difficult to parse. - * * Get this value as the given type. * * Supported types: object, array, raw_json_string, string_view, uint64_t, int64_t, double, bool * + * You may use get_double(), get_bool(), get_uint64(), get_int64(), + * get_object(), get_array(), get_raw_json_string(), or get_string() instead. + * * @returns A value of the given type, parsed from the JSON. * @returns INCORRECT_TYPE If the JSON value is not the given type. */ - template simdjson_really_inline simdjson_result get() & noexcept; + template simdjson_really_inline simdjson_result get() & noexcept { + // Unless the simdjson library provides an inline implementation, calling this method should + // immediately fail. + static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library."); + } /** @overload template simdjson_result get() & noexcept */ - template simdjson_really_inline simdjson_result get() && noexcept; + template simdjson_really_inline simdjson_result get() && noexcept { + // Unless the simdjson library provides an inline implementation, calling this method should + // immediately fail. + static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library."); + } /** * Get this value as the given type. diff --git a/include/simdjson/generic/ondemand/value.h b/include/simdjson/generic/ondemand/value.h index fdeeafe1..2c50f139 100644 --- a/include/simdjson/generic/ondemand/value.h +++ b/include/simdjson/generic/ondemand/value.h @@ -27,10 +27,17 @@ public: * * Supported types: object, array, raw_json_string, string_view, uint64_t, int64_t, double, bool * + * You may use get_double(), get_bool(), get_uint64(), get_int64(), + * get_object(), get_array(), get_raw_json_string(), or get_string() instead. + * * @returns A value of the given type, parsed from the JSON. * @returns INCORRECT_TYPE If the JSON value is not the given type. */ - template simdjson_really_inline simdjson_result get() noexcept; + template simdjson_really_inline simdjson_result get() noexcept { + // Unless the simdjson library provides an inline implementation, calling this method should + // immediately fail. + static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library."); + } /** * Get this value as the given type.