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