[Development] invokeMethod() with function pointers

Thiago Macieira thiago.macieira at intel.com
Fri Jan 20 03:01:54 CET 2017

On quinta-feira, 19 de janeiro de 2017 12:24:34 PST Benjamin TERRIER wrote:
> template <typename Func>
> static typename
> QtPrivate::QEnableIf<!QtPrivate::FunctionPointer<Func>::IsPointerToMemberFun
> ction && QtPrivate::FunctionPointer<Func>::ArgumentCount == -1
>   && !std::is_same<const char*, Func>::value, bool>::Type
>   invokeMethod(QObject *object, Func function)
> to QMetaObject breaks existing code and the auto tests in particular.
> In tst_qmetaobject.cpp there is this test:
> QVERIFY(!QMetaObject::invokeMethod(&obj, 0));
> because the new overload of invokeMethod() gets called this lead to
> compilation error instead
> of just returning false at run-time.
> To solve the issue one must add an explicit cast like so:
> QVERIFY(!QMetaObject::invokeMethod(&obj, (const char*)0));
> Note that casting to "char *" does not solve the issue.

Because it's a template, so the template when Func = char* matches better than 
the overload with const char*. I assume that using nullptr without casting 
also breaks, correct?

> Is this "source break"  acceptable (it might be looking at "Source
> break policy for function overloads" discussion and quip 6) and the
> autotests should be fixed?

>From what you explained, this will not affect the case when the pointer is a 
const char *, so neither

	const char *func = "deleteLater";
	QMetaObject::invokeMethod(&obj, func);


	const char *func = nullptr;
	QMetaObject::invokeMethod(&obj, func);

will stop compiling. So if you add an overload taking a non-const char* (and 
its unit test), we also catch the even more dubious code:

	char func[] = "deleteLater";
	QMetaObject::invokeMethod(&obj, func);

The only case that would break would be for a constant null pointer literal, 
which in my opinion is acceptable, since it should never happen in real code.

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Development mailing list