3

Based on this SO answer, I was experimenting with something similar but with a pointer:

#include <iostream>

class Bar {
public:
    virtual ~Bar() {}
};

class Foo: Bar {
public:
    Foo() { std::cout << "Foo::Foo()" << std::endl; }
    ~Foo() override { std::cout << "Foo::~Foo()" << std::endl; }
};

class Faz {
public:
    Faz() { std::cout << "Faz::Faz()" << std::endl; }
    ~Faz() { std::cout << "Faz::~Faz()" << std::endl; }
};

template <typename T>
typename std::enable_if<std::is_base_of<Bar, std::remove_pointer<T>>::value>::type
func(char const* type, T) {
    std::cout << type << " is derived from Bar" << std::endl;
}

template <typename T>
typename std::enable_if<!std::is_base_of<Bar, std::remove_pointer<T>>::value>::type
func(char const* type, T) {
    std::cout << type << " is NOT derived from Bar" << std::endl;
}

int main()
{
    func("std::unique_ptr<Foo>",  std::unique_ptr<Foo>());
    func("std::unique_ptr<Faz>",  std::unique_ptr<Faz>());
}

cout is :

std::unique_ptr<Foo> is NOT derived from Bar
std::unique_ptr<Faz> is NOT derived from Bar

Why does !std::is_base_of<Bar, type_identity<std::remove_pointer<T>>>::value always evaluate as true? I assumed (as a beginner):

std::unique_ptr<Foo> is derived from Bar
std::unique_ptr<Faz> is NOT derived from Bar

I'm probably missing something stupid.

Sean McVeigh
  • 569
  • 3
  • 18

1 Answers1

0
  1. std::remove_pointer<> acts on raw pointer types.
  2. The smart pointer's type is not related to the class hierarchy of referent type
WilliamClements
  • 350
  • 3
  • 8