Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Rationale behind not allowing template parameters to be friends?

Reply
Thread Tools

Rationale behind not allowing template parameters to be friends?

 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      04-05-2004
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/
 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      04-05-2004

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
message news:c4sfhp$mlo$(E-Mail Removed)...
> Consider the following code:



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

Jonathan


 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      04-05-2004
"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; }
> };



 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      04-06-2004
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/
 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      04-06-2004
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/
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      04-07-2004
"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: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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Rationale behind restrictions in full template specialization? Nick Bastin C++ 1 11-11-2007 04:07 AM
Rationale behind the deprecation of __getslice__? Fernando Perez Python 18 12-10-2004 09:42 PM
Allowing only certain classes as template parameters Helge Preuss C++ 2 07-22-2004 02:05 PM
Rationale behind requiring ampersand and fully qualified name to form a member function pointer Alexander Grigoriev C++ 0 09-12-2003 03:18 AM



Advertisments