template for "is static castable"

G

Gianni Mariani

I'm trying to make a generic factory that provides a placement new as
well as the accompanying destructor. To do a down-cast I used a
static_cast but it turns out that this is not always a possible because
of virtual inheritance. What I'd like to do is provide an automatic way
of deciding to use a dynamic or static cast depending on if it's
possible to static_cast.

The jist of what I want to do is below but I know it's wrong.

Any ideas ?


template <typename w_to, typename w_from, bool w_is_not = false>
struct is_static_castable
{
enum {
IsStaticCastable = 0
};
};

template <typename w_to, typename w_from>
struct is_static_castable<w_to, w_from, false>
{
enum {
IsStaticCastable = 0 < (int) static_cast< w_to >( (w_from) 0 )
};
};


struct Interface
{

int x;

};


struct Impl
: virtual Interface
{

int Impl;
};


#include <iostream>

int main()
{

std::cout << is_static_castable<Impl *, Interface
*>::IsStaticCastable << "\n";

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top