If you write something like this: QFuture<bool> thread_res = QtConcurrent::run([]() {});
under the hood there is template magic, and question is it valid c++ code.
1)template <typename T> class QFutureInterface
has method
QtPrivate::ResultStore<T> &resultStore()
{ return static_cast<QtPrivate::ResultStore<T> &>(resultStoreBase()); }
(Here's link to Qt sources to above method. )
2)resultStoreBase
return ResultStoreBase&
with such hieracity template <typename T> class ResultStore : public ResultStoreBase
, and ResultStoreBase&
really reference to ResultStoreBase object, NOT QtPrivate::ResultStore<T>
In other words we have something like this:
#include <map>
class Base {
public:
virtual ~Base() {}
protected:
std::map<int, std::string> results_;
};
template <typename T>
class Derived : public Base {
public:
void clear() {
results_.clear();
}
};
struct Foo {
Base base;
template <typename T>
Derived<T> &f() {
return static_cast<Derived<T> &>(base);
}
};
int main()
{
Foo foo;
foo.f<bool>().clear();
}
template<class T> class Derived
have no data, and foo.f<bool>()
used only to call methods, not to copy object or destroy object, so the only one method which can be called via vptr
- destructor never called via such interface.
So question is this code have undefined behavior?
According to this c++ design: cast from base to derived class with no extra data members such code have undefined behavior any way, but then why Qt guys wrote it in such way?