How to declare template member function of template class X as friend of nested class X::Y

余生颓废 提交于 2019-12-10 18:39:41

问题


I have a template class. It has a template function. Both take different template parameters. There is an internal class that needs to make a friend of the enclosing class's template function. Compiler errors abound. The following toy example shows my issue.

First, the following of course compiles (VS 2017):

template <typename T>
class Class1
{
    public:
        Class1() = default;
        ~Class1() = default;

        template <typename U>
        void Func(U& x) {};
};

class Class2
{
    public:
        Class2() = default;
        ~Class2() = default;

        template <typename T>
        template <typename U>
        friend void Class1<T>::Func(U& x);
};

int main()
{
    Class1<int> c1;
    return 0;
}

Now let's move Class2 into Class1 with no other changes:

template <typename T>
class Class1
{
    public:
        Class1() = default;
        ~Class1() = default;

        template <typename U>
        void Func(U& x){};

    class Class2
    {
        public:
            Class2() = default;
            ~Class2() = default;

            template <typename T> //Compiler error here.
            template <typename U>
            friend void Class1::Func(U& x);
    };
};

int main()
{
    Class1<int> c1;
    return 0;
}

Now I get a compiler error: error C3856: 'Class1<T>::Func': class is not a class template

I've played around with various ways to declare the friend when the class is nested, but I can't get it to compile. It's possible there is no way to do what I'm trying to do.

Note that the semantics of what I'm trying to do (in the real code, not this toy example) are such that Func should be a member function. This isn't about iterators or operators, which are often, of course, non-member functions. I've seen some similar questions to mine here, but they're often related to iterators or operators and I've not yet found a question with a solution that will work for me.

Worse comes to worse, it would be okay, given my design, to declare all of Class1 a friend of Class2 (and doing so lets me work around this issue). Class2 is a tiny helper class that is completely coupled to Class1; all of its special members, save the destructor and move ctor, are either private or deleted, and Class1::Func is the only thing that instantiates Class2 (and returns it via move ctor to users of Class1). So while it's not ideal to friend the entirety of Class1, it'd do in a pinch.

Thanks in advance.


回答1:


I first tried to reproduce OP's issue with gcc HEAD 9.0.0 on wandbox.org and got:

Start

prog.cc:17:23: error: declaration of template parameter 'T' shadows template parameter
17 |             template <typename T> //Compiler error here.
   |                       ^~~~~~~~
prog.cc:1:11: note: template parameter 'T' declared here
1 | template <typename T>
  |           ^~~~~~~~
prog.cc: In function 'int main()':
prog.cc:25:17: warning: unused variable 'c1' [-Wunused-variable]
25 |     Class1<int> c1;
   |                 ^~

1

Finish

The fix is simple – T is already a template parameter and must be renamed in the nested friend declaration:

template <typename T>
class Class1
{
    public:
        Class1() = default;
        ~Class1() = default;

        template <typename U>
        void Func(U& x){};

    class Class2
    {
        public:
            Class2() = default;
            ~Class2() = default;

            template <typename T1> //Compiler error gone.
            template <typename U>
            friend void Class1<T1>::Func(U& x);
    };
};

int main()
{
    Class1<int> c1;
    return 0;
}

Test on wandbox.org again:

Start

prog.cc: In function 'int main()':
prog.cc:25:17: warning: unused variable 'c1' [-Wunused-variable]
25 |     Class1<int> c1;
   |                 ^~

0

Finish

If it is intended that friend void Class1::Func(U& x); depends on the same template parameter T like Class1 this would be the alternative solution:

template <typename T>
class Class1
{
    public:
        Class1() = default;
        ~Class1() = default;

        template <typename U>
        void Func(U& x){};

    class Class2
    {
        public:
            Class2() = default;
            ~Class2() = default;

            template <typename U>
            friend void Class1::Func(U& x);
    };
};

int main()
{
    Class1<int> c1;
    return 0;
}

Tested on wandbox.org again:

Start

prog.cc: In function 'int main()':
prog.cc:24:17: warning: unused variable 'c1' [-Wunused-variable]
24 |     Class1<int> c1;
   |                 ^~

0

Finish



回答2:


It seems to be a bug in the MSVS compiler. Your code with Scheff's correction is accepted by both gcc and clang. As a temporary workaround #ifdef can be used:

#ifdef _MSC_VER
    friend class Class1;
#else
    template <typename U>
    friend void Class1::Func(U& x);
#endif

Addition. Bug report.



来源:https://stackoverflow.com/questions/51978275/how-to-declare-template-member-function-of-template-class-x-as-friend-of-nested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!