Rationale behind not allowing template parameters to be friends?



 DEVELOP > c-Plus-Plus > Rationale behind not allowing template parameters to be friends?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Asfand Yar Qazi"
Date: 05 Apr 2004 03:33:11 PM
Object: Rationale behind not allowing template parameters to be friends?
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/
.

User: "Jonathan Turkanis"

Title: Re: Rationale behind not allowing template parameters to be friends? 05 Apr 2004 03:43:34 PM
"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
message news:c4sfhp$mlo$1@news6.svr.pol.co.uk...

Consider the following code:

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

User: "Siemel Naran"

Title: Re: Rationale behind not allowing template parameters to be friends? 05 Apr 2004 04:01:47 PM
"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; }

};

.
User: "Asfand Yar Qazi"

Title: Re: Rationale behind not allowing template parameters to be friends? 06 Apr 2004 05:56:37 AM
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/
.
User: "Asfand Yar Qazi"

Title: Re: Rationale behind not allowing template parameters to be friends? 06 Apr 2004 06:03:37 AM
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/
.
User: "Siemel Naran"

Title: Re: Rationale behind not allowing template parameters to be friends? 06 Apr 2004 09:57:42 PM
"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::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.
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER