[PATCH] KSharedPtr operator== problem

Christoph Bartoschek bartoschek at gmx.de
Sat Oct 4 11:27:51 BST 2008


Am Samstag 04 Oktober 2008 schrieb Thiago Macieira:
> 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:
>
> ...
> 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.

Normally template friends are defined internally. Look at the following code, 
that works with gcc 4.2.1 (yours does not), EDG frontends and Comeau:

template<class T> class MyClass {
public:
    MyClass() : i(1) { }

    template <class X> void set(MyClass<X> & x) { 
      i = x.i; 
    }
    
    template <class X> friend class MyClass;
    
    
    //The friend is defined within the class. But this is not a
    //member function.
    friend void friendFunction(MyClass & t) { 
      ++t.i; 
    }
    
    void print() const {
      std::cout << "this = " << this << "  i = " << i << std::endl;
    }
    
private:
    T i;
};

int main() {
    MyClass<int> i;
    i.print();
    friendFunction(i);
    i.print();    
    MyClass<short> s;
    s.print();
    friendFunction(s);
    friendFunction(s);
    s.print();
    s.set(i);
    s.print();
}


Greetings
Christoph




More information about the kde-core-devel mailing list