vendredi 22 juillet 2016

How to use boost::intrusive_ptr for private nested class within class template

Suppose I have a list class:

template<typename T>
class list {
    ...

private:
    class node {
        ...

    private:
        std::size_t refcount_;

        // friends of node because accessing private member refcount_
        friend void intrusive_ptr_add_ref(const node* p) noexcept;
        friend void intrusive_ptr_release(const node* p) noexcept;            
    };

    // friends of list because accessing private nested class node
    friend void intrusive_ptr_add_ref(const node* p) noexcept;
    friend void intrusive_ptr_release(const node* p) noexcept;

    boost::intrusive_ptr<node> node_{new node};
};

template<typename T>
void intrusive_ptr_add_ref(const typename list<T>::node* p) noexcept
{ ... }

template<typename T>
void intrusive_ptr_release(const typename list<T>::node* p) noexcept
{ ... }

list<int> xs;  // error

The code above doesn't compile. The error was undefined symbols for intrusive_ptr_add_ref(list<int>::node const*) and intrusive_ptr_release(list<int>::node const*).

I think the problem is probably that I'm declaring non-template functions as friends in list and node, but what I defined are function templates. So what's the correct way to do this?

Aucun commentaire:

Enregistrer un commentaire