Connecting Tech Pros Worldwide Forums | Help | Site Map

friend with template class

Adam Badura
Guest
 
Posts: n/a
#1: Apr 23 '06
I have code like this

template<int size> big_int {
/* ... */
template<int size2> friend class big_int<size2>;
};

What I wanted to achive is to be able to easly convert different sized
big_int's and to operate on them. But for this I need to be able to access
private data of big_int. The problem is that for differenc sizes this are of
course different classes so I need friend declaration. But my compiler (MS
VS .NET 2005) does not accept this.
Is it possible? If no, how can I achive this except for declaring
private data public?

Thanks in advance.

Adam Badura



John Carson
Guest
 
Posts: n/a
#2: Apr 23 '06

re: friend with template class


"Adam Badura" <abadura@o2.pl> wrote in message
news:e2fn8d$48v$1@nemesis.news.tpi.pl[color=blue]
> I have code like this
>
> template<int size> big_int {
> /* ... */
> template<int size2> friend class big_int<size2>;
> };
>
> What I wanted to achive is to be able to easly convert different sized
> big_int's and to operate on them. But for this I need to be able to
> access private data of big_int. The problem is that for differenc
> sizes this are of course different classes so I need friend
> declaration. But my compiler (MS VS .NET 2005) does not accept this.
> Is it possible? If no, how can I achive this except for declaring
> private data public?[/color]


By using the correct syntax.

template<int size>
class big_int
{
/* ... */
template<int size2>
friend class big_int;
};


--
John Carson



Adam Badura
Guest
 
Posts: n/a
#3: Apr 23 '06

re: friend with template class


> By using the correct syntax.[color=blue]
>
> template<int size>
> class big_int
> {
> /* ... */
> template<int size2>
> friend class big_int;
> };[/color]

Hmmm....
Thanks. It worked. (As for my knowledge of C++ new lines after template
specifiers are not obligatory [and in my compiler {MS VS .NET 2005} it
worekd without them] - and this is just a style of writing code.)
Its funny, but I couldn't find any example in my books or on the Web
(but to be honset I didn't search very long). Perhabs it is not a popular
construct.
Thanks...

Adam Badura


John Carson
Guest
 
Posts: n/a
#4: Apr 23 '06

re: friend with template class


"Adam Badura" <abadura@o2.pl> wrote in message
news:e2g6im$931$1@atlantis.news.tpi.pl[color=blue][color=green]
>> By using the correct syntax.
>>
>> template<int size>
>> class big_int
>> {
>> /* ... */
>> template<int size2>
>> friend class big_int;
>> };[/color]
>
> Hmmm....
> Thanks. It worked. (As for my knowledge of C++ new lines after
> template specifiers are not obligatory [and in my compiler {MS VS
> .NET 2005} it worekd without them] - and this is just a style of
> writing code.)[/color]

Indeed, the new lines are purely stylistic.

The corrections of substance are:

1. Adding the missing class identifier before the first big_int.

2. Deleting the <size2> after the second big_int.


--
John Carson


Adam Badura
Guest
 
Posts: n/a
#5: Apr 23 '06

re: friend with template class


> 1. Adding the missing class identifier before the first big_int.

Ohh...
This was a simple mistake. I didn't paste this code from my projcet but
write it by hand and forgot about that...
[color=blue]
> 2. Deleting the <size2> after the second big_int.[/color]

So as I supposed form your post. But why? I do not understend why is
this not specified? What if I wanted to be friend only to even sized
numbers? Something like this:

template<int size2> friend class big_int<size*2>;

now I cannot do this without specifing the specialization. And what is more
code like

friend class big_int<size*2>;

(for size begin template parametr in the class itself) works fine.

And besides it is strange to omit this <size2> if we specify for which
parameters this should be specialized. And even more, whatfor is
template<int size2> now?

Adam Badura


John Carson
Guest
 
Posts: n/a
#6: Apr 24 '06

re: friend with template class


"Adam Badura" <abadura@o2.pl> wrote in message
news:e2gds6$309$1@atlantis.news.tpi.pl[color=blue][color=green]
>> 1. Adding the missing class identifier before the first big_int.[/color]
>
> Ohh...
> This was a simple mistake. I didn't paste this code from my
> projcet but write it by hand[/color]

Always a really bad idea.
[color=blue]
>[color=green]
>> 2. Deleting the <size2> after the second big_int.[/color]
>
> So as I supposed form your post. But why?[/color]

Because the C++ standard says so.
[color=blue]
> I do not understend why
> is this not specified? What if I wanted to be friend only to even
> sized numbers? Something like this:
>
> template<int size2> friend class big_int<size*2>;
>
> now I cannot do this without specifing the specialization.[/color]

This type of specialization is illegal at any time. It has nothing to do
with friendship. Try:

template<int size>
class Foo
{};

template<int size>
class Foo<size*2>
{};

This won't compile. You aren't allowed to form algebraic expressions out of
the template parameters when partially specializing.
[color=blue]
> And what is more code like
>
> friend class big_int<size*2>;
>
> (for size begin template parametr in the class itself) works fine.[/color]

Sure, that means that if you declare

big_int<5> bi;

then you are granting friendship to instances of big_int<10>. You are not
partially specializing.
[color=blue]
> And besides it is strange to omit this <size2> if we specify for
> which parameters this should be specialized. And even more, whatfor is
> template<int size2> now?[/color]

With

template<int size>
class big_int
{
/* ... */
template<int n>
friend class big_int;
};

when you declare

big_int<5> bi;

you make big_int<n> a friend for every int n. This is an "unbound friend"
declaration, i.e., the friend doesn't need to have the same parameter.

With

template<int size>
class big_int
{
/* ... */
friend class big_int;
};


when you declare

big_int<5> bi;

you only make big_int<5> a friend. This is a "bound friend" declaration,
i.e., the friend must have the same parameter value.

If you are going to make extensive use of templates, I recommend C++
Templates: The Complete Guide by David Vandevoorde and Nicolai Josuttis.

--
John Carson


Adam Badura
Guest
 
Posts: n/a
#7: Apr 24 '06

re: friend with template class


> If you are going to make extensive use of templates, I recommend C++[color=blue]
> Templates: The Complete Guide by David Vandevoorde and Nicolai Josuttis.[/color]

OK. Greate thanks...
To be honest I think I know C++ :), but this is something I have never
used (and as I have writen I couldn't find it in Stroustrup for example). So
now I will have to search for some info in this area with your replyes as a
quide. Thanks.

Adam Badura


Tom Widmer
Guest
 
Posts: n/a
#8: Apr 24 '06

re: friend with template class


Adam Badura wrote:[color=blue][color=green]
>>2. Deleting the <size2> after the second big_int.[/color]
>
>
> So as I supposed form your post. But why? I do not understend why is
> this not specified? What if I wanted to be friend only to even sized
> numbers? Something like this:
>
> template<int size2> friend class big_int<size*2>;[/color]

size*2 doesn't depend on size2. Is that really what you meant?
[color=blue]
>
> now I cannot do this without specifing the specialization. And what is more
> code like
>
> friend class big_int<size*2>;
>
> (for size begin template parametr in the class itself) works fine.[/color]

Yes,
friend class big_int<size*2>;
is perfectly valid. The two cases are different - in one case you are
declaring friendship for all specializations of a template, in the other
case just a particular specialization. You'd expect the syntax to be
different, no?

One thing you can't do though is declare friendship for a partial
specialization, and there's no particularly good reason for that, it's
just the way it is. (e.g. this is illegal:
template<int size2> friend class big_int<size2*2>;
as an attempt to declare friendship only to even specializations).
[color=blue]
> And besides it is strange to omit this <size2> if we specify for which
> parameters this should be specialized. And even more, whatfor is
> template<int size2> now?[/color]

To show that every specialization is being granted friendship.

Tom
Adam Badura
Guest
 
Posts: n/a
#9: Apr 24 '06

re: friend with template class


>> template<int size2> friend class big_int<size*2>;[color=blue]
>
> size*2 doesn't depend on size2. Is that really what you meant?[/color]

No. Of course i meant

template<int size2> friend class big_int<size2*2>;
[color=blue]
> One thing you can't do though is declare friendship for a partial
> specialization, and there's no particularly good reason for that, it's
> just the way it is. (e.g. this is illegal:
> template<int size2> friend class big_int<size2*2>;
> as an attempt to declare friendship only to even specializations).[/color]

OK. And that is the thing I wanted to know. (Really I needed only this
what I got in first reply, but as the whole thing started I want to get as
much of it as I can :).) However it seems kind of strang that this is
illegal - but on the other hand this is not the only thing strange... :)
[color=blue]
> To show that every specialization is being granted friendship.[/color]

But why couldn't it be written like this

friend class big_int;

Is it because in this case this means by default big_int<size> (where size
is ta paramter name for the class) like for declaring functions?

Adam Badura


Tom Widmer
Guest
 
Posts: n/a
#10: Apr 24 '06

re: friend with template class


Adam Badura wrote:
[color=blue][color=green]
>>To show that every specialization is being granted friendship.[/color]
>
>
> But why couldn't it be written like this
>
> friend class big_int;[/color]

Well, in the general case you don't know whether big_int is a template
or not, since it might not have been declared yet, so the syntax with
the template <...> bit makes it explicit whether it is a template or
just a plain class that is being granted friendship.

Where big_int is a the class itself, then you have the name-injection
problem you mentioned.

But syntax is just an arbitrary choice in many cases - the "why" is
often not very interesting.

Tom
Closed Thread