Skip to content

Compatibility with std::string_view #1448

Description

@pwsoftwaredesigns

I ran into a compatiblity issue between etl and the standard library when working with strings. My existing code uses std::string_view (instead of etl::string_view). It appears that ETL has no conversion between the two types so I cannot, for example, do the following:

etl::string<64> myString;
std::string_view myStringView; //Assume this is provided somewhere else

myString += myStringView;

I noticed the following preprocessor test in string_view.h:

#if ETL_USING_STL && ETL_USING_CPP17
  #include <string_view>
#endif

But, other than include std::string_view, it doesn't seem to do anything with it.

My solution was to make the following changes:

In string_view.h
Wrap the basic_string_view class in a preprocessor check that simply causes etl::basic_string_view to std::basic_string_view

#if ETL_USING_STL && ETL_USING_CPP17
  template <typename T, typename TTraits = std::char_traits<T> >
  using basic_string_view = std::basic_string_view<T, TTraits>;
#else
  template <typename T, typename TTraits = etl::char_traits<T> >
  class basic_string_view
  {
  //EXISTING CODE...

In basic_string.h
Add an implicit conversion operator in ibasic_string to etl::basic_string_view

template <typename T>
class ibasic_string : public::string_base
{
//EXISTING CODE...

//*************************************************************************
/// Conversion operator to etl::basic_string_view.
//*************************************************************************
operator etl::basic_string_view<T>() const
{
  return etl::basic_string_view<T>(p_buffer, size());
}

That way, if std::string_view is available, ETL just uses it directly.

The other option I can think of would be to explicitly add conversion operators and constructors for std::string_view (along with etl::string_view) to etl::ibasic_string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions