[Interest] QStringView implementation for Qt 5.9?

René J.V. Bertin rjvbertin at gmail.com
Fri Jul 19 15:08:14 CEST 2019


Hi,

I've been coming across code that uses QStringView but that I'd like to be able to build against Qt 5.9 . I have thus been thinking of ways to achieve this and am hoping on some constructive feedback (= not of the kind "just use Qt 5.1x already ;) )

I haven't seen a clear, succinct description of what QStringView can do that QString can't, I get the impression it is above all a thin wrapper around (wide) strings that does a lot less than QString and so is (somewhat?) faster.
If correct, I *think* that something like

```
class QStringView : public QString
{
public:
	QStringView(const QString &that)
	{
		*(static_cast<QString*>(this)) = that;
	}
       const QString toString() const
	{
		auto deepcopy = QString();
		deepcopy.append(this);
		return deepcopy;
	}
};
```

could be a transparent alternative, possibly with a cast-to-QString operator and an assignment operator. Any method overrides added in other classes that take a QStringView argument instead of a QString should become unnecessary thanks to the inheritance and the copy ctor ... or am I overlooking something?

Alternatively I could probably extract the QStringView class from Qt 5.10 (or later) plus anything (not in 5.9) it depends, add the cast-to-QString operator and bundle all that in a small library. That should give access to some of the advantages that QStringView must have over QString, provided they don't get lost by the more frequent conversions ... and that QStringView does not depend on private QString implementation details.

The 1st approach is simple enough to just try and see where things strand, the 2nd approach will require a bit more investment, hence this message.

Thanks in advance!
R.



More information about the Interest mailing list