2009/6/30 Sakharuk, Vlad (GMI Development)
Here is example:
class one {
public:
typedef someclass internal;
};
class two {
// No internal class
};
class default_internal{
};
I would like to be able to write something if it possible:
template class proc {
};
So I can use proc with both
proc<one> One;
proc<two> Two;
without specifying derived type?
#include <iostream>
#include
#include
#include
struct someclass {
static void hello() {
std::cout << "someclass" << std::endl;
}
};
struct one {
public:
typedef someclass internal;
};
struct two {};
struct default_internal {
static void hello() {
std::cout << "default_internal" << std::endl;
}
};
BOOST_MPL_HAS_XXX_TRAIT_DEF(internal)
// Here Internal is either T::internal
// if it exists or default_internal.
template
struct proc_impl {
static void hi() {
Internal::hello();
}
};
template <class T>
struct get_internal {
typedef typename T::internal type;
};
template <class T>
struct proc :
proc_impl<
T,
typename boost::mpl::eval_if<
has_internal<T>,
get_internal<T>,
boost::mpl::identity
>::type
{
};
int main() {
proc<one>::hi();
proc<two>::hi();
}
Roman Perepelitsa.