Connecting Tech Pros Worldwide Help | Site Map

Rationale behind not allowing template parameters to be friends?

  #1  
Old July 22nd, 2005, 10:00 AM
Asfand Yar Qazi
Guest
 
Posts: n/a
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/
  #2  
Old July 22nd, 2005, 10:00 AM
Jonathan Turkanis
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?



"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
message news:c4sfhp$mlo$1@news6.svr.pol.co.uk...[color=blue]
> Consider the following code:[/color]


Here's an article on the subject:
http://www.cuj.com/documents/s=8942/cujweb0312wilson/.

Jonathan


  #3  
Old July 22nd, 2005, 10:00 AM
Jonathan Turkanis
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?



"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
message news:c4sfhp$mlo$1@news6.svr.pol.co.uk...[color=blue]
> Consider the following code:[/color]


Here's an article on the subject:
http://www.cuj.com/documents/s=8942/cujweb0312wilson/.

Jonathan


  #4  
Old July 22nd, 2005, 10:00 AM
Siemel Naran
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
[color=blue]
> 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;
> }[/color]

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.

[color=blue]
> friend OwnerT;
>
> ValueT data;[/color]

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;
}



[color=blue]
> class Big
> {[/color]
class Private_Restricted_Public_Variable { };[color=blue]
> public:
> Big(int arg)
> : myint(arg)
> {
> }
>
> Restricted_Public_Variable<int, Big> myint;[/color]
void f() { getdata(Private_Restricted_Public_Variable()) = 3; }[color=blue]
> };[/color]


  #5  
Old July 22nd, 2005, 10:00 AM
Siemel Naran
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
[color=blue]
> 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;
> }[/color]

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.

[color=blue]
> friend OwnerT;
>
> ValueT data;[/color]

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;
}



[color=blue]
> class Big
> {[/color]
class Private_Restricted_Public_Variable { };[color=blue]
> public:
> Big(int arg)
> : myint(arg)
> {
> }
>
> Restricted_Public_Variable<int, Big> myint;[/color]
void f() { getdata(Private_Restricted_Public_Variable()) = 3; }[color=blue]
> };[/color]


  #6  
Old July 22nd, 2005, 10:01 AM
Asfand Yar Qazi
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


Siemel Naran wrote:[color=blue]
> "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
>
>[/color]
<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/
  #7  
Old July 22nd, 2005, 10:01 AM
Asfand Yar Qazi
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


Siemel Naran wrote:[color=blue]
> "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
>
>[/color]
<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/
  #8  
Old July 22nd, 2005, 10:01 AM
Asfand Yar Qazi
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


Asfand Yar Qazi wrote:[color=blue]
> Siemel Naran wrote:
>[color=green]
>> "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
>> message
>>
>>[/color]
> <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.)
>[/color]

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/
  #9  
Old July 22nd, 2005, 10:01 AM
Asfand Yar Qazi
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


Asfand Yar Qazi wrote:[color=blue]
> Siemel Naran wrote:
>[color=green]
>> "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
>> message
>>
>>[/color]
> <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.)
>[/color]

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/
  #10  
Old July 22nd, 2005, 10:03 AM
Siemel Naran
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
[color=blue]
> 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.)[/color]

Sure, you can derive publicly from Restricted_Public_Variable, so all users
can take advantage of Restricted_Public_Variable::operator() 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.


  #11  
Old July 22nd, 2005, 10:03 AM
Siemel Naran
Guest
 
Posts: n/a

re: Rationale behind not allowing template parameters to be friends?


"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
[color=blue]
> 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.)[/color]

Sure, you can derive publicly from Restricted_Public_Variable, so all users
can take advantage of Restricted_Public_Variable::operator() 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.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rationale behind not allowing template parameters to be friends? Asfand Yar Qazi answers 10 July 22nd, 2005 09:34 AM