From: eric.robert on
I need a policy that can call back the main interface. I tried with
the following code but I do not understand why I cannot access the
member T::Type (see commented code) in some cases but not in others. I
get the same error on VC and GCC (see below). Is there a nice way to
fix this?

struct MyPolicy
{
template<typename T>
struct Body
{
// this doesn't work...
//void f0(typename T::Type item) {}

// and this doesn't work too...
//typedef typename T::Type MyType;
//void f1(MyType item) {}

// but all this is fine...
void foo()
{
int k = sizeof(typename T::Type);
typename T::Type item = k;

T * ptr = static_cast<T *>(this);
ptr->on_foo(item);
}
};
};

template<typename T, typename Policy = MyPolicy>
struct MyClass
: public Policy::template Body<MyClass<T, Policy> >
{
typedef T Type;
typedef typename Policy::template Body<MyClass> PolicyType;

void some_function()
{
PolicyType::foo();
}

void on_foo(size_t k)
{
std::cout << "size: " << k << std::endl;
}
};

int main(int argc, char ** argv)
{
MyClass<int> test;
test.some_function();
return 0;
}

VC:
error C2039: 'Type' : is not a member of 'MyClass<T>'

GCC:
main.cpp: In instantiation of `MyPolicy::Body<MyClass<int, MyPolicy>
>':
main.cpp:30: instantiated from `MyClass<int, MyPolicy>'
main.cpp:47: instantiated from here
main.cpp:9: error: no type named `Type' in `struct MyClass<int,
MyPolicy>'

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: juanalday on
On Jan 9, 6:47 pm, juanal...(a)gmail.com wrote:
> On Jan 9, 10:42 am, eric.rob...(a)videotron.ca wrote:
>
> > I need a policy that can call back the main interface. I tried with
> > the following code but I do not understand why I cannot access the
> > member T::Type (see commented code) in some cases but not in others. I
> > get the same error on VC and GCC (see below). Is there a nice way to
> > fix this?
>
> Apart from a missing #include <iostream> statement, the code is valid.
> I can compile it (and run it) properly under both VS 2005 & gcc (3.4.2
> & 4.1.1)
>
> What version of the compilers are you using?
>
Never mind. Rough day at work. I didn't even read your code properly.
Mod. Please cancel my prev post if you still haven't pushed it.

{ in most cases we can't cancel approved posts; they are processed in
order received, usually, and upon approval are propagated to Usenet
immediately. -mod }

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: juanalday on
On Jan 9, 10:42 am, eric.rob...(a)videotron.ca wrote:
> I need a policy that can call back the main interface. I tried with
> the following code but I do not understand why I cannot access the
> member T::Type (see commented code) in some cases but not in others. I
> get the same error on VC and GCC (see below). Is there a nice way to
> fix this?
>
Apologies for my previous reply. Didn't read your example properly. I
only compiled it and assumed you had problems with your compiler :(

The problem is that your base class MyPolicy::Body cannot be fully
defined, as it depends on MyClass.
void foo() doesn't have these problems because whatever is inside is
not part of the declaration of the structure itself.
A solution (although I don't think that's your original intent) is to
change the definition of Body to
template<typename R, typename T> struct Body
{
typedef R MyType;
....
};
and derive MyClass from Body<T, MyClass<T, Policy> >

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]