BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_stringviewlikeparam

Detailed Description

Provide macros for SFINAE constraints on string-view-like params.

Outline

Purpose

Provide macros for SFINAE constraints on string-view-like params.

Macros

See also
bslstl_isconvertibletostringview, bslstl_isconvertibletocstring

Description

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.

Macro Summary

This section provides a brief description of each macro:

Usage

This section illustrates intended use of this component.

Example 1: Declaring a String-View-Like Constructor

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:

template <class STRING_VIEW_LIKE_TYPE>
explicit MyClass(const STRING_VIEW_LIKE_TYPE& value,
#define BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(...)
Definition bslstl_stringviewlikeparam.h:159

Then, we define the constructor outside the class:

template <class STRING_VIEW_LIKE_TYPE>
MyClass::MyClass(const STRING_VIEW_LIKE_TYPE& value,
{
bsl::string_view sv = value;
// use sv...
}
Definition bslstl_stringview.h:471

Macro Definition Documentation

◆ BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T

#define BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T (   ...)
Value:
typename bsl::enable_if< \
BloombergLP::bslstl::IsConvertibleToStringView< \
CHAR_TYPE, \
CHAR_TRAITS, \
__VA_ARGS__>::type
#define BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE
Definition bslstl_stringviewlikeparam.h:132
Definition bslmf_enableif.h:530

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.

◆ BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T

#define BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T (   ...)
Value:
typename bsl::enable_if< \
BloombergLP::bslstl::IsConvertibleToStringView< \
CHAR_TYPE, \
CHAR_TRAITS, \
&& !BloombergLP::bslstl::IsConvertibleToCString< \
CHAR_TYPE, \
__VA_ARGS__>::type

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.

◆ BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE

#define BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE
Value:
typename bsl::enable_if<0 != sizeof(STRING_VIEW_LIKE_TYPE), \
const STRING_VIEW_LIKE_TYPE&>::type

We need to use an intermediate completeness test to work around deficiencies with SFINAE in the Sun and AIX compilers.