bound template parameter as argument to friend member function declaration

From: Justin Miller (millerj_at_milkyway.gsfc.nasa.gov)
Date: 11/01/04


Date: Mon, 1 Nov 2004 17:13:55 -0500

Ok, I tried to make that subject as descriptive as possible.

What I'm trying to do:

I'm attempting to use policies to create a generic memento (design
pattern) template. My Memento template so far is pretty simple:

using an idea of Andrei Alexandrescu (at least that's where I first read
it) to create a lightweight way of overloading member functions since
member function specialization is not possible.

template <typename T>
struct Type2Type {
    typedef T OriginalType;
};

template <typename Originator,
          typename CmdObj,
          typename StorageType>
class Memento
{
public:
    friend Memento Originator::CreateMemento(Type2Type<CmdObj>);
//other junk
};

My point in doing this is to force a compile-time check that Originator
has a CreateMemento function that takes an argument of type Type2Type<T>.
The main reason I decided to make just the member function CreateMemento a
friend is because a statement like this:

friend class Originator; //or this...
friend Originator;

is illegal in c++. If it's an entire class we're making a friend, the
friend syntax specifies that you have to include the word 'class.'
However, in this case, adorning the template parameter Originator, with
the word 'class,' is illegal (I forget the exact reason, but regardless,
not the point).

The problem:

I instantiate this template like this:

typedef Memento<DataContainer, DataCmdObj, std::vector<SpectralData*> >
    DataMemento;

When I compile this with Sun Workshop 7, it results in an error, saying
that in the friend declaration above:

friend Memento Originator::CreateMemento(Type2Type<CmdObj>);

CmdObj is not defined. This really doesn't seem like it should be a
problem. Any thoughts here?!

Could it be a problem with forward declarations vs. the need for the whole
interface to be visible?

This has been bugging me big time, so any help is appreciated.

Justin

-----------------------------------------

Programmer/Analyst
NASA/Goddard Space Flight Center Code 660
email: millerj@milkyway.gsfc.nasa.gov
office: 301-286-9261



Relevant Pages

  • Re: Circular Class Template Friendship
    ... > My issue is the syntax of declaring a friend class within ... > which the string is a fixed width that is specialized. ... it is still possible to have Name_Id_Table template ... the friend class declaration. ...
    (comp.lang.cpp)
  • Re: Class templates and friend function templates
    ... > It never occurred to me that the friend declaration could be an overload. ... > template ... I suspect the online version uses an EDG option to "instantiate ...
    (microsoft.public.vc.language)
  • Re: Template friendship to nested template class
    ... friend struct INNER; ... Probably you are missing an EXTRA template to declare ... I think it's because the name referred to in a 'friend' declaration is looked up to exist at the namespace scope. ...
    (microsoft.public.vc.language)
  • Re: Template friendship to nested template class
    ... template ... friend struct INNER; ... Both VC and gcc are happy with this code, but the intel compiler is ...
    (microsoft.public.vc.language)
  • Re: Class templates and friend function templates
    ... friend void process; ... each specialization of the `task' class template has all specializations of the function template `func' as friends. ... It only has one parameter that is distinct, thus implying a kind of partial specialisation of the friend declaration once C is instantiated for a particular type. ...
    (microsoft.public.vc.language)

Loading