Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Understanding qAsConst and std::as_const

class MyClass {
  QStringList m_list;
public:
  void print() {
     for (const QString &str : m_list)  // WARNING: might detach, add qAsConst
        print(str);
  }
};
class MyClass {
  QString m_list;
public:
  void print() {
     const QStringList copy = m_list;
     for (const QString &str : copy) // OK
        print(str);
  }
};
class MyClass {
  QStringList m_list;
public:
  void print() const {
     for (const QString &str : m_list)
        print(str);
  }
};
class MyClass {
  QStringList m_list;
public:
  void print() {
     for (const QString &str : std::as_const(m_list))
        print(str);
  }
};
QStringList generateStrings();

void print() {
   for (const QString &str : generateStrings()) // Clazy warning here
      print(str);
}
QStringList generateStrings();

void print() {
  for (const QString &str : qAsConst(generateStrings())) // ERROR
    print(str);
}
QStringList generateStrings();

void print() {
  const QStringList list = generateStrings();
  for (const QString &str : list) // OK
    print(str);
}
QStringList generateStrings();

void print() {
  for (const QStringList list = generateStrings(); const QString &str : list) // OK
    print(str);
}
QStringList generateStrings() {
  QStringList result;
  result << "hello" << "world";
  return result;
}

void print() {
   for (const QString &str : generateStrings()) // Why a warning? The returned list isn't shared with anyone!
      print(str);
}
const QStringList generateStrings();

void print() {
   for (const QString &str : generateStrings()) // OK
      print(str);
}
std::vector<QString> generateStrings();

void print() {
   for (const QString &str : generateStrings()) // OK
      print(str);
}

About KDAB


6 Comments

14 - Nov - 2022

Steve

15 - Nov - 2022

Giuseppe D'Angelo

30 - Nov - 2022

Martin Koller

30 - Nov - 2022

Giuseppe D'Angelo

4 - Dec - 2022

Vlad

QStringList generateStrings();

template <typename T>
inline const T moveToConst(T &&t)
{
    return std::move(t);
}

void print() {
   for (const QString &str : moveToConst(generateStrings()))
      print(str);
}

5 - Dec - 2022

Giuseppe D'Angelo

template <typename T>
// lvalues
const T &make_const(const T &t) { return t; }

// rvalues -- need to make it not work with the universal reference
template <typename T, std::enable_if_t<!std::is_reference_v<T>, bool> = true>
const T make_const(T &&t) { return std::move(t); }

for (const QString &s :    make_const(stringList)      ) { print(s); } // stringList is not moved-from
for (const QString &s :    make_const(getStringList()) ) { print(s); } // return of getStringList() moved into make_const's return object
for (const QString &s :    stringList   | views::as_const) { print(s); }
for (const QString &s : getStringList() | views::as_const) { print(s); }
for (const QString &s :        vector   | views::as_const) { print(s); }
for (const QString &s :     getVector() | views::as_const) { print(s); }
JesperKjaerPedersen

Jesper K. Pedersen

HR Director / COO

Learn Modern C++

Learn more