- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
是否有可能静态断言作为模板参数提供的类型是否实现了参数包中列出的所有类型,即。参数包感知 std::is_base_of()?
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert(is_base_of<Requirements..., Type>::value, "Invalid.");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parameter pack aware version of std::is_base_of()
public:
template <typename T> T* as()
{
static_assert(std::is_base_of<Requirements..., T>::value, "Invalid.");
return reinterpret_cast<T*>(this);
}
};
最佳答案
C++17 更新:使用 C++17 的折叠表达式,这几乎变得微不足道:
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert((std::is_base_of_v<Type, Requirements> && ...), "Invalid.");
};
原始答案 (C++11/14):您可能会使用包扩展和一些静态版本的 std::all_of
:
template <bool... b> struct static_all_of;
//implementation: recurse, if the first argument is true
template <bool... tail>
struct static_all_of<true, tail...> : static_all_of<tail...> {};
//end recursion if first argument is false -
template <bool... tail>
struct static_all_of<false, tail...> : std::false_type {};
// - or if no more arguments
template <> struct static_all_of<> : std::true_type {};
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert(static_all_of<std::is_base_of<Type, Requirements>::value...>::value, "Invalid.");
// pack expansion: ^^^
};
struct Base {};
struct Derived1 : Base {};
struct Derived2 : Base {};
struct NotDerived {};
int main()
{
CommonBase <Base, Derived1, Derived2> ok;
CommonBase <Base, Derived1, NotDerived, Derived2> error;
}
包扩展将扩展为您通过在 Requirements...
中插入每种类型而获得的值列表对于 std::is_base_of<Type, ?>::value
中的问号,即对于 main 中的第一行,它将扩展为 static_all_of<true, true>
, 对于第二行,它将是 static_all_of<true, false, true>
关于c++ - 参数包感知 std::is_base_of(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13562823/
我试图断言模板参数将派生自某个基类。但是基类是通用的,在断言的上下文中,任何特化类型之间都没有区别。我如何断言模板参数派生自任何专用类型的泛型? 我试着把它写成 base_generic: templ
给定代码: template struct Cat { ... }; template struct Bengal : Cat { ... }; template struct Persian : C
以下代码是如何工作的? typedef char (&yes)[1];typedef char (&no)[2];template <typename B, typ
我正在研究共享指针的实现。 (使用C++ 17,以防万一) 唯一的问题是转换构造函数。我希望能够将smart_ptr静态转换为基本类型的smart_ptr。 template inline smart
我有这样的层次结构: namespace MyService{ class IBase { public: virtual ~IBase(){} protected: IPointer
只是想知道为什么我不能像下面这样在 C++ 模板参数中使用 is_base_of: #include #include using namespace std; struct base { };
有没有办法判断一个基类是不是虚基类? std::is_base_of 将标识一个基类,但我正在寻找类似 std::is_virtual_base_of 的东西来标识一个虚拟基类。 这是出于 SFINA
有没有办法测试std::is_base_of什么时候A是模板类吗? template class A {}; template class B : public A {}; 我想静态测试类似 st
我正在尝试做一个结构类似于此的程序: #include template class Test {}; template class Test::value>::type> { public:
是否有可能静态断言作为模板参数提供的类型是否实现了参数包中列出的所有类型,即。参数包感知 std::is_base_of()? template class CommonBase { sta
使用下面的代码,我收到编译器投诉,称前两个模板之间对 get_code 的调用不明确。我如何编写代码来检测基类,同时还提供专门的形式?例如,如果稍后我有 class C : A {},它也应该返回 A
1. template std::true_type is_base_of_test_func( Base* ); 2. template std::false_type is_base_of
有没有办法测试std::is_base_of当A是模板类吗? template class A {}; template class B : public A {}; 我想静态测试 std::is
我对以下情况感到有点困惑,我在 is_base_of 上启用了特化。 is_base_of 需要被检查的类型的完整定义是可用的。但是,正在专门化的类型被用作正在检查的基类类型的成员 - 因此两者都需要
我遇到了模板问题: 我有两个构造函数和方法:.cpp: Cell::Cell(sf::Vector2i& uPos, sf::Vector2f& cellDimensions, std::string
我想创建一个库:- 用户通过 addCallback(Callback* callback) 添加回调(通常在第一个时间步) 之后,通常在不同的.cpp , 每当用户调用 actor() :- 如果B
我正在思考一个关于如何 is_base_of 的好问题在 boost 中实现(它决定一个给定的 class 在编译时是否是另一个 class 的基础)。 第一次看到这样的代码,我很惊讶怎么能把事情做得
我完全明白为什么这个不能工作了: class Base {}; class A; static_assert(std::is_base_of::value, ""); 因为没有关于“类层次结构”的信息
根据 C++11 标准 std::is_base_of如果 D 是从 B 派生的,或者两者是同一个非 union 类,则必须返回 true。但是在 VC++ 2010 STL 实现模板中,如果类型 B
为简洁起见,这是简化的层次结构: class IBase { public: virtual ~IBase() = 0 { }; }; // eo IBase class IDerived
我是一名优秀的程序员,十分优秀!