如何将 SFINAE 与 child class 一起使用

How to use SFINAE with a child class

我有一个 non-template parent 界面,我正在尝试在 child class 上使用 SFINAE。我想要做的就是验证第一个模板参数的类型可以在没有任何参数的情况下构造。我的问题是前向声明。根据我的理解,SFINAE 需要前向声明 class 并 then 专门化。这是我目前正在尝试的:

class ParentInterface
{};

template<class, class = void>
class Child : public ParentInterface; // <-- This semi-colon is the error

template<class Ty>
class Child<Ty, std::enable_if_t<std::is_constructible_v<Ty>>>
    : public ParentInterface
{};

我能够找到的与此相关的答案与继承 SFINAE class有关。我需要做什么才能获得预期的功能?

template<class, class = void> class Child : public ParentInterface;

既不是声明也不是定义。

您可能需要声明:

`template<class, class = void>
class Child;

然后您可以添加偏特化定义:

template<class Ty>
class Child<Ty, std::enable_if_t<std::is_constructible_v<Ty>>>
    : public ParentInterface
{};