Can a template template argument from another namespace be a friend?

I apologize if the title of this question is less useful; I do not know a short way to ask this question without specifying the following example:

template <template <class> class Arg>
class C {
    typedef C<Arg> type;
    friend class Arg<type>;
  public:
    C() {
        a_.set(this);
    }
  private:
    int i_;
    Arg<type> a_;
};

template <class Type>
class Arg1 {
  public:
    void set(Type* t) {
        t_ = t;
        t_->i_ = 1;
    }
  private:
    Type* t_;
};

namespace NS {

    template <class Type>
    class Arg2 {
      public:
        void set(Type* t) {
            t_ = t;
            t_->i_ = 2;
        }
      private:
        Type* t_;
    };

}

As you can see, Arg2is a copy Arg1. However, VS 2008 allows you Arg1to use as a template argument:

int main() {
    C<Arg1> c1; // compiles ok
    C<NS::Arg2> c2; // error C2248

    return 0;
}

The error 'C<Arg>::i_' : cannot access private member declared in class 'C<Arg>'. Everything works fine if i_it becomes publicly available, so this seems to be a friendship issue.

What causes a friendship declaration to fail when the template template argument is in a different namespace?

+5
source share
1 answer

Membership in the namespace does not affect the right to friendship. This is a compiler error.

friend namespace - , , , . , , ::Arg2<type>.

0

All Articles