How to test at compile time whether class B is derived from std::vector?
template
struct is_derived_from_vector {
static const bool value =
I had the same situation that I needed to know if a class is derived from a vector(like)-class. Unfortunately there is no C++-11 or variadic macros allowed in my project. So my solution was a mixture of Kerrek's answer and this article with some googletest-code at the end:
#include
template
class is_derived_from_vector
{
typedef char Yes_t[1];
typedef char No_t[2];
static No_t& test(const void* const);
template
static Yes_t& test(const std::vector* const);
public:
static const bool value = ((sizeof(test(static_cast(0)))) == (sizeof(Yes_t)));
};
template struct X {};
struct A : X {};
struct B : std::vector {};
TEST(Example, IsDerivedFrom)
{
EXPECT_FALSE(is_derived_from_vector::value);
EXPECT_TRUE(is_derived_from_vector::value);
}
A common solution for any templates I think would not be possible to define without usage of C++-11 or higher.