Is it possible to mark an alias template as a friend?
Asked Answered
V

2

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)

Valdemar answered 6/11, 2015 at 9:26 Comment(9)
try it (spoiler alert: you can)Transpadane
@bolov: With which syntax ? I tried both template <class> friend class BeFriend and template <class> friend BeFriend with no success on Clang 3.6, so a little more details would go a long way here :)Valdemar
I spoke too soon. I thought it was trivial. My bad.Transpadane
@Transpadane 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 ;)Valdemar
I need to watch that movie again. The spoiler wasn't there :)). Tried like 10 combinations, nothing works.Transpadane
@Transpadane Haha ;) I am afraid this movie does not have a happy-ending, but let's use this question to make sure.Valdemar
If there is a way to do it, the committee doesn't know about it. See the note at the bottom of CWG 1554.Something
@T.C.: thank you, that could probably be the (sad) answer to this question.Valdemar
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.Samanthasamanthia
M
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;
Maratha answered 6/11, 2015 at 10:12 Comment(0)
N
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
Niagara answered 6/11, 2015 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.