By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

template for "is static castable"

P: n/a


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";

}

Jul 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.