[PATCH] KSharedPtr operator== problem

Thiago Macieira thiago at kde.org
Sat Oct 4 10:55:31 BST 2008


Adriaan de Groot wrote:
>On Saturday 04 October 2008 09:29:12 Thiago Macieira wrote:
>> I asked our C++ standard experts in-house about the subject. Roberto
>> couldn't come up with a definitive answer: we can't decide whether the
>> standard allows it or not.
>
>OK. At some point "does it compile with the tools we have?" trumps "does
> the standard allow it?" anyway. There must be some subtlety I'm missing
> there.
>
>What would help in this specific case is a complete test program (from
> Alex) (with no dependency on an installed KDE, if possible) that
> exercises this stuff so we can just throw it at the non-gcc compilers
> and see what happens.

Try this:

template<class T> class MyClass;
template<class T> void friendFunction(MyClass<T> &t);

template<class T> class MyClass
{
    template<class X> friend void friendFunction(MyClass<T> t);
    template<class X> friend class MyClass;
    T i;
public:
    MyClass() : i(1) { }

    template<class X> void set(MyClass<X> x)
    { i = x.i; }
};

template<class T> void friendFunction(MyClass<T> &t)
{ ++t.i; }

int main()
{
    MyClass<int> i;
    friendFunction(i);

    MyClass<short> s;
    s.set(i);
}

This works fine in GCC 4.3, but fails in 3.3.6:
/dev/stdin: In function `void friendFunction(MyClass<T>&) [with T = int]':
/dev/stdin:19:   instantiated from here
/dev/stdin:5: error: `int MyClass<int>::i' is private
/dev/stdin:14: error: within this context

I added the macro Q_NO_TEMPLATE_FRIENDS to qglobal.h to test this compiler 
feature.

BTW, QSharedPointer also requires partial template specialisation and 
member template functions. That excludes a lot of old compilers from using 
it already.

-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20081004/74acaee9/attachment.sig>


More information about the kde-core-devel mailing list