Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

QStringView Diaries: Masters Of The Overloads

How QStringView actively manages implicit conversions


QStringView() : m_data(nullptr), m_size(0) {}
QStringView(std::nullptr_t) : QStringView() {}
#ifdef Q_COMPILER_UNICODE_STRINGS
QStringView(const char16_t *data, qssize_t size) : m_data(data), m_size(size) {}
QStringView(const char16_t *data) : QStringView(data, data ? lengthHelper(data) : 0) {}
#endif
#ifdef Q_OS_WIN
QStringView(const wchar_t *data /** BOOOOOOOOOOORING!! **/

template <typename Char>
using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleChar<Char>::value>::type*;

template <typename String>
using if_compatible_stdstring = typename std::enable_if<QtPrivate::IsCompatibleStdBasicString<String>::value>::type*;

QStringView() : m_data(nullptr), m_size(0) {}
QStringView(std::nullptr_t) : QStringView() {}

template <typename Char, typename = if_compatible_char<Char>>
QStringView(const Char *data, qssize_t size) : m_data(castHelper(data)), m_size(size) {}

template <typename Char, typename = if_compatible_char<Char>>
QStringView(const Char *data) : m_data(castHelper(data)), m_size(data ? lengthHelper(data) : 0) {}

template <typename String, typename = if_compatible_stdstring<String>>
QStringView(const String &str) : QStringView(str.data(), qssize_t(str.size())) {}

QStringView(const QString &str) : QStringView(str.isNull() ? nullptr : str.data(), qssize_t(str.size()) {}
QStringView(const QStringRef &str) : QStringView(str.isNull() ? nullptr : str.data(), qssize_t(str.size()) {}
 tst_qstringview.cpp(119) : error C2338: CanConvert<std::wstring>::value == CanConvertFromWCharT
 tst_qstringview.cpp(120) : error C2338: CanConvert<const std::wstring>::value == CanConvertFromWCharT
 tst_qstringview.cpp(121) : error C2338: CanConvert<std::wstring&>::value == CanConvertFromWCharT
 tst_qstringview.cpp(122) : error C2338: CanConvert<const std::wstring&>::value == CanConvertFromWCharT

template <typename Char>
using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleChar<Char>::value, bool>::type;

template <typename String>
using if_compatible_stdstring = typename std::enable_if<QtPrivate::IsCompatibleStdBasicString<String>::value, bool>::type;

template <typename Char, if_compatible_char<Char> = true>
QStringView(const Char *data, qssize_t size) : m_data(castHelper(data)), m_size(size) {}

QStringView(QLatin1String) = delete;
QStringView(const char *) = delete;
template <typename Char, if_compatible_char<Char> = true>
QStringView(Char) = delete;
// ...

bool isValidIdentifier(const QString &id); // pre-existing
bool isValidIdentifier(QStringView id);    // newly added

isValidIdentifier(QLatin1String("QString")); // OK, implicitly converts to QString
isValidIdentifier("QString");                // OK, ditto, unless QT_NO_CAST_FROM_ASCII is defined
isValidIdentifier(QChar('x'));               // OK, ditto

template <typename QStringLike, if_compatible_qstring_like<QStringLike> = true>
QStingView(const QStringLike &str) : QStringView(str.isNull() ? nullptr : str.data(), qssize_t(str.size())) {}

template <typename QStringLike ...>
QString(const QStringLike &str);
template <typename StdString ...>
QString(const StdString &str);

isValidIdentifier(u"QString");

  template <typename Char>
#if __cplusplus >= 201402L // C++14
  constexpr
#endif
  qssize_t lengthHelper(const Char *data)
  {
      qssize_t result = 0;
      while (*data++)
          ++result;
      return result;
  }
  template <typename Char, if_compatible_char<Char> = true>
  constexpr QStringView(const Char *str) noexcept : QStringView(str, str ? lengthHelper(str) : 0) {}

template <typename Char, qssize_t N, if_compatible_char<Char> = true>
constexpr QStringView(const Char (&array)[N]) : QStringView(array, N - 1) {}

template <typename Array, if_compatible_array<Array> = true>
QStringView(const Array &array) : QStringView(array, std::size(array) - 1) {}
template <typename Pointer, if_compatible_pointer<Pointer> = true>
QStringView(const Pointer &ptr) : QStringView(ptr, ptr ? lengthHelper(ptr) : 0) {}

5 Comments

2 - May - 2017

Michael Vlach

3 - May - 2017

Pedantic?

3 - May - 2017

Marc Mutz

6 - May - 2017

Johannes Schaub

QStringView(const Char * const& data)

6 - May - 2017

Johannes Schaub

char x[255];
sprintf(x, "not %d chars", 254);

QStringView qv = x;