Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template friend with local classes ?

41 views
Skip to first unread message

Helmut Jarausch

unread,
Feb 3, 2014, 11:17:57 AM2/3/14
to
Hi

I'd like to replace the following definition with a typedef version

typedef double Scalar;

class Term {
class Add;
public:
Term() {}
virtual Scalar operator()(Scalar Arg) const { return Arg; }

friend Add& operator+(const Term& A, const Term& B);
};

by a template class

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// something like the following declaration is necessary,
// but how to pre-declare the local class Add of class Term

template <typename Scalar>
typename Term<Scalar>::Add& operator+(const Term<Scalar>& A, const Term<Scalar>& B);

template <typename Scalar>
class Term {
class Add;
public:
Term() {}
virtual Scalar operator()(Scalar Arg) const { return Arg; }

friend Add<Scalar>& operator+(const Term<Scalar>& A, const Term<Scalar>& B);
};

Is there a solution for my problem?

Many thanks for any hints,
Helmut

Luca Risolia

unread,
Feb 3, 2014, 1:48:51 PM2/3/14
to
Helmut Jarausch wrote:
> // something like the following declaration is necessary,
> // but how to pre-declare the local class Add of class Term
>
> template <typename Scalar>
> typename Term<Scalar>::Add& operator+(const Term<Scalar>& A, const
> Term<Scalar>& B);
>
> template <typename Scalar>
> class Term {
> class Add;
> public:
> Term() {}
> virtual Scalar operator()(Scalar Arg) const { return Arg; }
>
> friend Add<Scalar>& operator+(const Term<Scalar>& A, const Term<Scalar>&
> B);
> };
>
> Is there a solution for my problem?

template<typename Scalar> class Term;

template <typename Scalar>
typename Term<Scalar>::template Add<Scalar>& operator+(const Term<Scalar>& A,
const Term<Scalar>& B);

template <typename Scalar>
class Term {
template <typename T>
class Add { /*...*/};
public:
Term() {}
virtual Scalar operator()(Scalar Arg) const { return Arg; }

friend Add<Scalar>& operator+<>(const Term& A, const Term& B);
};

Helmut Jarausch

unread,
Feb 4, 2014, 5:37:12 AM2/4/14
to
Many thanks, Luca.
I still have a question about your solution, see below.
Helmut

On Mon, 03 Feb 2014 19:48:51 +0100, Luca Risolia wrote:

> Helmut Jarausch wrote:
>> // something like the following declaration is necessary,
>> // but how to pre-declare the local class Add of class Term
>>
>> template <typename Scalar>
>> typename Term<Scalar>::Add& operator+(const Term<Scalar>& A, const
>> Term<Scalar>& B);
>>
>> template <typename Scalar>
>> class Term {
>> class Add;
>> public:
>> Term() {}
>> virtual Scalar operator()(Scalar Arg) const { return Arg; }
>>
>> friend Add<Scalar>& operator+(const Term<Scalar>& A, const Term<Scalar>&
>> B);
>> };
>>
>> Is there a solution for my problem?



> template<typename Scalar> class Term;
>
> template <typename Scalar>
> typename Term<Scalar>::template Add<Scalar>& operator+(const Term<Scalar>& A,
> const Term<Scalar>& B);
>
> template <typename Scalar>
> class Term {
> template <typename T> // ???????????? why do I need this if I will never instantiate
> class Add { /*...*/}; // a Term object with a nested Add object with a different template parameter?

Luca Risolia

unread,
Feb 4, 2014, 12:25:58 PM2/4/14
to
Helmut Jarausch wrote:
>> template <typename T> // ???????????? why do I need this if I will
>> never instantiate
>> class Add { /*...*/}; // a Term object with a nested Add object with a
>> different template parameter?

..because you used Add like template class in your friend function
declaration.

If you don't want Add to be a template class, just make it non-template and
modify the function declarations accordingly. It's left as an exercise.

0 new messages