http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53926
Bug #: 53926
Summary: g++ fails to compile valid template code with casting
to QVariant and back
Classification: Unclassified
Product: gcc
Version: 4.8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
Trying to compile the following code:
#include <QtCore/QSettings>
template <typename T>
void bug (const T& default_value)
{
QSettings settings;
settings.value ("foo", default_value).value <T> ();
}
results in compilation failure:
$ gcc -I/usr/include/qt4 -c gcc-bug.cpp
gcc-bug.cpp: In function ‘void bug(const T&)’:
gcc-bug.cpp:7:49: error: expected primary-expression before ‘>’ token
gcc-bug.cpp:7:52: error: expected primary-expression before ‘)’ token
The error can be reproduced in a small code piece without any includes:
class Variant
{
public:
Variant (int) { }
template <typename T>
T value () const { return T (); }
};
template <typename T>
Variant get_variant (const T& x)
{
return Variant (x);
}
template <typename T>
void bug (const T& x)
{
get_variant (x).value <T> ();
}
$ gcc -I/usr/include/qt4 -c gcc-bug.cpp
gcc-bug.cpp: In function ‘void bug(const T&)’:
gcc-bug.cpp:19:27: error: expected primary-expression before ‘>’ token
gcc-bug.cpp:19:30: error: expected primary-expression before ‘)’ token
Splitting the line
get_variant (x).value <T> ();
into
Variant tmp = get_variant (x);
tmp.value <T> ();
eliminates the error.
The error is reproducible in 4.2.4, 4.5.1, 4.6.3 and 4.8.1 prerelease versions.
ICC & MSVC compile these examples without errors or warnings.