Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I declaring partially specialized friend class? - very confused

Tags:

c++

templates

I've been twiddling my thumbs over this one for far too much time now. I am trying to implement a single linked list using two different allocators for both the nodes and the type to which they point. The following code keeps complaining at me that I am partially specializing the friend class declaration within the SingleListNode definition:

namespace containers {
template<typename T, typename TAlloc,
typename NAlloc>class SingleList; // forward declaration

template<typename T, typename TAlloc = std::allocator<T>>
class SingleListNode {
    template<typename T1, typename T2, typename T3>
    friend class SingleList<T1, T2, T3> ; // partially specialized???
    // class definition
};

template<typename T, typename TAlloc = std::allocator<T>,
        typename NAlloc = std::allocator<SingleListNode<T>>>
class SingleList {
    // class definition
};
} // end of namespace containers

Keeps telling me:

../src/singlelist.h:21:16: error: specialization of ‘template struct containers::SingleList’ must appear at namespace scope ../src/singlelist.h:21:39: error: partial specialization ‘containers::SingleList’ declared ‘friend’

So far as I can tell, this isn't a specialization. Perhaps it is a bug in the GCC compiler? Otherwise, where am I going wrong?

like image 718
Ian Haggerty Avatar asked Dec 07 '11 15:12

Ian Haggerty


People also ask

Can we declare a template function as the friend of the class?

Template friendsBoth function template and class template declarations may appear with the friend specifier in any non-local class or class template (although only function templates may be defined within the class or class template that is granting friendship).

What does template typename t mean?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.


1 Answers

You are declaring a templated friend class, so the correct syntax is

 template<typename T1, typename T2, typename T3> 
 friend class SingleList;

without the <T1, T2, T3> after SingleList. See, for instance, the "practical usage example" here

like image 161
bbtrb Avatar answered Nov 15 '22 12:11

bbtrb