|
BDE 4.39.x Production Release
|
Provide macros for SFINAE constraints on string-view-like params.
Provide macros for SFINAE constraints on string-view-like params.
This component provides a set of macros that simplify the declaration of function template parameters constrained to accept "string-view-like" types. A type is considered "string-view-like" if it is convertible to bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>.
The "ONLY" variants additionally exclude types that are convertible to const CHAR_TYPE * (C-strings), which is useful for overload resolution when separate overloads exist for C-string arguments.
These macros are designed to be used in function declarations and definitions where SFINAE is needed to constrain template parameters.
This section provides a brief description of each macro:
BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE: Helper macro that provides a completeness check for the STRING_VIEW_LIKE_TYPE template parameter, working around SFINAE deficiencies in certain compilers.BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(ResultType): SFINAE enable_if type alias for types convertible to string_view. The result is ResultType if enabled. Use as: BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(void) * = 0 for declaration, or BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(const ALLOCATOR&) alloc = ALLOCATOR()BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(ResultType): SFINAE enable_if type alias for types convertible to string_view but NOT convertible to C-string (const CHAR_TYPE *). The result is ResultType if enabled. Use as: BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(void) * = 0 for declaration, or BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(const ALLOCATOR&) alloc = ALLOCATOR()This section illustrates intended use of this component.
Suppose we want to declare a constructor that accepts any type convertible to bsl::string_view but not const char *.
First, we declare the constructor in the class definition:
Then, we define the constructor outside the class:
| #define BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T | ( | ... | ) |
Expand to an enable_if type alias for ResultType that is defined (i.e., SFINAE-friendly) only when the STRING_VIEW_LIKE_TYPE template parameter is convertible to bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>. The CHAR_TYPE and CHAR_TRAITS template parameters must be in scope. The specified ResultType (passed as a variadic argument) is the resulting type when the condition is satisfied.
| #define BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T | ( | ... | ) |
Expand to an enable_if type alias for ResultType that is defined (i.e., SFINAE-friendly) only when the STRING_VIEW_LIKE_TYPE template parameter is convertible to bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS> but is not convertible to const CHAR_TYPE *. The CHAR_TYPE and CHAR_TRAITS template parameters must be in scope. The specified ResultType (passed as a variadic argument) is the resulting type when both conditions are satisfied.
| #define BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE |
We need to use an intermediate completeness test to work around deficiencies with SFINAE in the Sun and AIX compilers.