13

Imagine we have this code:

template <class, class>
class Element
{};

template <class T>
class Util
{
public:
   template <class U>
   using BeFriend = Element<T, U>;
};

Is it possible to mark BeFriend as a friend ? (Of Util, or any other class).

 Edit

The "obvious" syntax were tried, but both failed with Clang 3.6.

template <class> friend class BeFriend;
template <class> friend BeFriend;

I did not know about the second syntax, but found it in this answer. It seems to work (and be required) for non-template aliases, but does not help in this case where the alias is templated.

(Note: as some could infer from the minimal example, I am looking for a way to work-around the limitation that C++ does not allow to friend a partial template specialization)

Community
  • 1
  • 1
Ad N
  • 7,930
  • 6
  • 36
  • 80
  • 3
    try it (spoiler alert: you can) – bolov Nov 06 '15 at 09:28
  • @bolov: With which syntax ? I tried both `template friend class BeFriend` and `template friend BeFriend` with no success on Clang 3.6, so a little more details would go a long way here :) – Ad N Nov 06 '15 at 09:29
  • I spoke too soon. I thought it was trivial. My bad. – bolov Nov 06 '15 at 09:34
  • @bolov Okay no problem ! I like the fact that people are upvoting your comment based on the fact it is very assertive, even though they probably do not appreciate the problem at hand ;) – Ad N Nov 06 '15 at 09:35
  • I need to watch that movie again. The spoiler wasn't there :)). Tried like 10 combinations, nothing works. – bolov Nov 06 '15 at 09:45
  • @bolov Haha ;) I am afraid this movie does not have a happy-ending, but let's use this question to make sure. – Ad N Nov 06 '15 at 09:47
  • 4
    If there is a way to do it, the committee doesn't know about it. See the note at the bottom of [CWG 1554](http://wg21.link/CWG1554). – T.C. Nov 06 '15 at 09:52
  • @T.C.: thank you, that could probably be the (sad) answer to this question. – Ad N Nov 06 '15 at 09:54
  • 1
    The sad answer is the answer, as far as I can tell - there's clear (enough) wording about what can be a friend in the relevant clause in the Standard and alias templates aren't in there, so that looks like a dead end. However, there could be a workaround for your underlying question (simulating a partial specialization friend), if you're willing to change the way `Element` is defined - it would involve nested class templates. Not sure if it would help you. It looks [like this](http://melpon.org/wandbox/permlink/l2wQiOwLSEmL61uj). – bogdan Nov 06 '15 at 17:03

2 Answers2

9

I think you can't do that because partial specializations could not be declared as friend.

From the stardard, [temp.friend]/7

Friend declarations shall not declare partial specializations. [ Example:

template<class T> class A { };
class X {
  template<class T> friend class A<T*>; // error
};

—end example ]

You have to specify a more generic version such as:

template <class, class> friend class Element;

or a full specified version, such as:

using BeFriend = Element<T, int>;
friend BeFriend;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

The problem is not the alias, the problem is that "partial specialization cannot be declared as a friend".

template <class, class> friend class Element;        // OK

template <class, class> friend class Element<T, T>;  // Error
sergej
  • 17,147
  • 6
  • 52
  • 89