Skip to content

Unify handling of string-like arguments across the API #1745

Description

@legendecas

Follow-up to #1741 / #1742.

Several APIs still carry multiple overloads for different string types. Now that the library requires C++17, we can modernize by preferring std::string_view and removing the redundant UTF-8 overloads, replacing them with a single constrained template, as a semver-major change.

For String::New, Symbol::New, and Symbol::For, collapse the const std::string& + std::string_view pair into:

template <typename T,
          std::enable_if_t<
              std::is_convertible_v<const T&, std::string_view> &&
              !std::is_convertible_v<const T&, const char*>, int> = 0>
static String New(napi_env env, const T& t);

This matches the C++17 standard-library idiom (std::string's find/append/compare/… members). The !is_convertible_v<const T&, const char*> clause keeps const char* and literals on the dedicated overload and filters nullptr (avoiding a UB string_view(nullptr)).

The enable_if alias is shareable across APIs e.g. enable_if_string_view_like_t<T>.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions