【问题标题】:std::is_base_of for a ctor compiling errorstd::is_base_of 用于 ctor 编译错误
【发布时间】:2023-03-22 05:37:01
【问题描述】:

已编辑

我有以下几点:

#include <iostream>
#include <vector>
#include <type_traits>

class IListableItem {};
class SubIListableItem : public IListableItem {};
class Base{};
template<class T> struct is_listable_item : public std::false_type {};
template<class T> struct is_listable_item<std::is_base_of<IListableItem, T>> : public std::true_type {};
template<class T, std::enable_if_t<is_listable_item<T>::value, bool> = true>
class ImplementationOfBase : public Base
{
public:
    explicit ImplementationOfBase(const std::vector<T> &items): Base(){}
    virtual ~ImplementationOfBase(){}

protected:
    std::vector<std::wstring> getLabels(const std::vector<T> &source)
    {
    std::vector<std::wstring> ret;
    for (auto &i : source) ret.emplace_back(i.label);
    return ret;
    }
};

class Page : public ImplementationOfBase<SubIListableItem> {};

int main ()
{

}

它返回我:

prog.cc:34:58: error: no type named 'type' in 'struct std::enable_if<false, bool>'
   34 | class Page : public ImplementationOfBase<SubIListableItem> {};
      |                                                          ^

在这种情况下我不能使用指针 有什么线索吗?

现场示例: https://wandbox.org/permlink/ESviQ0qHrjLPX9wE

谢谢。

【问题讨论】:

  • 编译时遇到什么错误?
  • 错误 C2993:'std::enable_if<:is_base_of>::value,bool>':非类型模板参数 '__formal' 的非法类型
  • 您使用的是哪个编译器?这对我来说在 Windows 中使用 clang 和 intel 编译器编译得很好。
  • 它也是compiles for me
  • 这不是使用 SFINAE 进行类声明的方式...查看在线指南。

标签: c++ c++11 templates sfinae


【解决方案1】:

你可以改变is_listable_item的定义如下:

template<class T> struct is_listable_item : public std::is_base_of<IListableItem, T> {};

您的代码不起作用,因为 is_listable_item 已专门用于 std::is_base_of&lt;IListableItem, T&gt;,但您正在传递 SubIListableItem

【讨论】:

    猜你喜欢
    • 2016-04-12
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多