P
pastbin
Dear members,
The following code compiles and runs fine on MSVC++ 2005 Express
edition.
Warning level at maximum and nothing wrong is reported.
This is a minimal code I wrote in order to test something for my
personal needs.
I'm not sure if the code is "legal" because I'm downcasting in a
constructor of a base class
to call a member of another base class of a different branch in the
hierarchy.
//------------
#include <iostream>
template<typename Derived>
struct aspect
{
aspect() { // constructor
Derived* derived = static_cast<Derived*>(this); // downcast
derived->handler_member(); // call a member of a base class of
derived
}
};
template<typename Derived>
struct handler
{
void handler_member() {
std::cout << "handler";
}
};
template<typename Derived, template<class> class Handler>
struct win : public Handler<Derived>
{
};
struct my_win : public win<my_win, handler>
, public aspect<my_win>
{
void dummy() {
std::cout << "ok";
}
};
int main() {
my_win w; // print "handler" as expected
w.dummy(); // print "ok" as expected
return 0;
}
//------------
Isn't this code somewhat esoteric ?
Thank you
mark
The following code compiles and runs fine on MSVC++ 2005 Express
edition.
Warning level at maximum and nothing wrong is reported.
This is a minimal code I wrote in order to test something for my
personal needs.
I'm not sure if the code is "legal" because I'm downcasting in a
constructor of a base class
to call a member of another base class of a different branch in the
hierarchy.
//------------
#include <iostream>
template<typename Derived>
struct aspect
{
aspect() { // constructor
Derived* derived = static_cast<Derived*>(this); // downcast
derived->handler_member(); // call a member of a base class of
derived
}
};
template<typename Derived>
struct handler
{
void handler_member() {
std::cout << "handler";
}
};
template<typename Derived, template<class> class Handler>
struct win : public Handler<Derived>
{
};
struct my_win : public win<my_win, handler>
, public aspect<my_win>
{
void dummy() {
std::cout << "ok";
}
};
int main() {
my_win w; // print "handler" as expected
w.dummy(); // print "ok" as expected
return 0;
}
//------------
Isn't this code somewhat esoteric ?
Thank you
mark