Rationale behind not allowing template parameters to be friends?

Discussion in 'C++' started by Asfand Yar Qazi, Apr 5, 2004.

  1. Consider the following code:

    =========================================

    #include <iostream>

    template<class ValueT, OwnerT>
    class Restricted_Public_Variable
    {
    public:
    typedef Restricted_Public_Variable Self;

    private:
    Restricted_Public_Variable()
    : data()
    {
    }

    virtual
    ~Restricted_Public_Variable()
    {
    }

    Restricted_Public_Variable(const ValueT& arg)
    : data(arg)
    {
    }

    Restricted_Public_Variable(const Self& arg)
    : data(arg.data)
    {
    }

    const Self&
    operator=(const ValueT& arg)
    {
    data = arg;
    return *this;
    }

    friend OwnerT;

    ValueT data;

    public:
    operator ValueT()
    {
    return data;
    }

    const ValueT&
    get() const
    {
    return data;
    }
    };

    class Big
    {
    public:
    Big(int arg)
    : myint(arg)
    {
    }

    Restricted_Public_Variable<int, Big> myint;
    };

    int
    main()
    {
    Big b(3);

    std::cout << b.myint << std::endl; // 3

    // The following is disallowed, we can
    // only read the 'myint' value
    // b.myint = 5;
    }

    =========================================

    Yet the compiler (gcc 3.3.3) says:

    template parameters cannot be friends

    Bah foiled...

    Can anybody explain why this is?

    --
    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Apr 5, 2004
    #1
    1. Advertising

  2. Jonathan Turkanis, Apr 5, 2004
    #2
    1. Advertising

  3. Asfand Yar Qazi

    Siemel Naran Guest

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message

    > template<class ValueT, OwnerT>
    > class Restricted_Public_Variable
    > {
    > public:
    > typedef Restricted_Public_Variable Self;
    >
    > private:
    > Restricted_Public_Variable()
    > : data()
    > {
    > }
    >
    > virtual
    > ~Restricted_Public_Variable()
    > {
    > }
    >
    > Restricted_Public_Variable(const ValueT& arg)
    > : data(arg)
    > {
    > }
    >
    > Restricted_Public_Variable(const Self& arg)
    > : data(arg.data)
    > {
    > }
    >
    > const Self&
    > operator=(const ValueT& arg)
    > {
    > data = arg;
    > return *this;
    > }


    Compiler generated copy constructor, operator=, destructor are fine. It
    makes sense to define any of these functions anyway if you want them to be
    non-inline. I usually do it for the virtual destructor only though.


    > friend OwnerT;
    >
    > ValueT data;


    It's not allowed at present. I think there was a proposal to make it legal,
    but maybe I am mistaken. As a workaround you can create a public function
    that returns a writable reference to data, and the function will take an
    OwnerT::Restricted_Public_Variable or other class by value. But the default
    constructor of this class will be private so that only members of OwnerT
    will be able to create OwnerT::Restricted_Public_Variable objects.

    ValueT& getdata(OwnerT::Restricted_Public_Variable) {
    return data;
    }




    > class Big
    > {

    class Private_Restricted_Public_Variable { };
    > public:
    > Big(int arg)
    > : myint(arg)
    > {
    > }
    >
    > Restricted_Public_Variable<int, Big> myint;

    void f() { getdata(Private_Restricted_Public_Variable()) = 3; }
    > };
     
    Siemel Naran, Apr 5, 2004
    #3
  4. Siemel Naran wrote:
    > "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    >
    >

    <snip>

    Thanks both - but I think I'll use Siemel's version :) Hopefully it
    works on all compilers, unlike the forms identified in the article
    Jonathan linked to (although it was a very enlightening article.)

    Thanks both,
    Asfand Yar


    --
    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Apr 6, 2004
    #4
  5. Asfand Yar Qazi wrote:
    > Siemel Naran wrote:
    >
    >> "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
    >> message
    >>
    >>

    > <snip>
    >
    > Thanks both - but I think I'll use Siemel's version :) Hopefully it
    > works on all compilers, unlike the forms identified in the article
    > Jonathan linked to (although it was a very enlightening article.)
    >


    Hang on a mo Siemel... I think you've misunderstood what I was trying to
    do. I'm trying to create a public variable that is publicly readable,
    but only the owner can write to (similar to how Eiffel defines its
    variables.)

    Sorry about that, thanks anyway,
    Asfand Yar

    --
    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Apr 6, 2004
    #5
  6. Asfand Yar Qazi

    Siemel Naran Guest

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message

    > Hang on a mo Siemel... I think you've misunderstood what I was trying to
    > do. I'm trying to create a public variable that is publicly readable,
    > but only the owner can write to (similar to how Eiffel defines its
    > variables.)


    Sure, you can derive publicly from Restricted_Public_Variable, so all users
    can take advantage of Restricted_Public_Variable::eek:perator() which returns a
    readable reference. But only users with ability to create the private class
    can make use of the getdata() function which returns a writable reference.
     
    Siemel Naran, Apr 7, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Grigoriev
    Replies:
    0
    Views:
    407
    Alexander Grigoriev
    Sep 12, 2003
  2. Fernando Perez
    Replies:
    18
    Views:
    669
    Steven Bethard
    Dec 10, 2004
  3. Replies:
    2
    Views:
    617
  4. Replies:
    0
    Views:
    683
  5. Nick Bastin
    Replies:
    1
    Views:
    308
    Jack Klein
    Nov 11, 2007
Loading...

Share This Page