templates and friends

F

F. P. Beekhof

Hello,

could anybody tell what I'm doing wrong?

template <typename T, std::size_t D>
class DTree;

template <class is, typename T, std::size_t D>
is& operator>>(is& i_stream, DTree<T, D> &tree);

template <typename T, std::size_t D>
class DTreeProxy
{
public:
template <class is, typename TT, std::size_t DD>
friend is& operator>> (is& i_stream, DTree<T, D> &tree);

private: typedef int Blah;
};


template <class is, typename T, std::size_t D>
is& operator>>(is& i_stream, DTree<T, D> &tree)
{
typedef typename DTreeProxy<T, D>::Blah Blah; // Error
}

The error is then that Blah is private in the operator>>() context.

I'm lost. How should it be done ?

Many thanks in advance,

Fokko Beekhof
 
L

litb

Hello,

could anybody tell what I'm doing wrong?

template <typename T, std::size_t D>
class DTree;

template <class is, typename T, std::size_t D>
is& operator>>(is& i_stream, DTree<T, D> &tree);

template <typename T, std::size_t D>
class DTreeProxy
{
        public:
                template <class is, typename TT, std::size_t DD>
                friend is& operator>> (is& i_stream, DTree<T, D> &tree);

        private: typedef int Blah;

};

template <class is, typename T, std::size_t D>
is& operator>>(is& i_stream, DTree<T, D> &tree)
{
        typedef typename DTreeProxy<T, D>::Blah Blah; // Error

}

The error is then that Blah is private in the operator>>() context.

I'm lost. How should it be done ?

The bug lies here: You have to use DD and TT - not D and T in the
parameter list of the function.

template <class is, typename TT, std::size_t DD>
friend is& operator>> (is& i_stream, DTree<TT, DD> &tree);

As far as i know, it's not possible to declare a friend given a
partial set of template arguments. Either you have to declare
particular instances as friends, or all possible instances of a
template. The above declares all ones as friends. The below declares
one particular instance as friend.

friend istream& operator>> <>(istream& i_stream, DTree<T, D> &tree);

Now, whether the specialization was instantiated or explicitly
specialized - it will be a friend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top