3

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.

Loss Mentality
  • 364
  • 4
  • 11
  • 1
    I just recognized that `template void Class1::Func(U& x) {}` is `public`. So, just drop the `friend` stuff. (I'm joking.) Your MCVE would make more sense if it were `private`. I assume that was the actual intention. – Scheff's Cat Aug 23 '18 at 06:19
  • @scheff Ha, yes, this is just a toy example, so access level of certain class members aren't what they are in the real code. – Loss Mentality Aug 23 '18 at 20:00

2 Answers2

3

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
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • It seems to be a MSVS bug that OP's code (with `template` in the `friend` declaration being removed) does not compile. – Evg Aug 23 '18 at 05:57
  • @Evgeny Did you try it in VS2017? (I did only with g++ where it worked as expected.) I missed that in OP's question and assumed he/she didn't check that (2nd working variant). – Scheff's Cat Aug 23 '18 at 06:00
  • I tried it in the latest beta VS2018. In your first block you probably missed `` in `friend void Class1::Func(U& x);`. – Evg Aug 23 '18 at 06:02
  • @Evgeny AFAIK, if template argument is left out then (inside definition of `Class1`) `Class1` === `Class1`. I've to admit that this doesn't make much sense together with `template `... – Scheff's Cat Aug 23 '18 at 06:06
  • That's correct. I thought your intention was to make all `Func`s from all `Class1`s friends. – Evg Aug 23 '18 at 06:11
  • @Evgeny Meanwhile I've played with this on VS2013. In the past, I had some rare occurrences of `friend template`s where I hadn't problems with. That's why I'm wondering a bit that VS cannot compile this properly. I tried as well with forward declaration and definition of `Class1::Class2` afterwards but it didn't change anything. – Scheff's Cat Aug 23 '18 at 06:15
  • 1
    @scheff Thanks to you and everyone - I had tried changing the template parameter to something different (along with trying a bunch of other things), but it still hadn't worked. It appears that that's due to a bug in VS. The shame of it is the real version of this code is normally compiled with gcc, but I had left my gcc computer at work and was screwing with the code at home. So I wouldn't even have had to write this post if I'd not been forgetful. Thank again. – Loss Mentality Aug 23 '18 at 20:03
  • @scheff - Woudl you mind commenting on my question - do you believe that it is the same issue? Thank you [link](https://stackoverflow.com/questions/52101122/compiling-a-c-program-that-compiles-with-gcc-and-not-microsoft-cl?noredirect=1#comment91153511_52101122) – scalauser Aug 30 '18 at 17:12
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.

Evg
  • 25,259
  • 5
  • 41
  • 83