- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我完全明白为什么这个不能工作了:
class Base {};
class A;
static_assert(std::is_base_of<Base, A>::value, "");
因为没有关于“类层次结构”的信息,但是...为什么以下不能工作?
class Base {};
class A : public Base {
static_assert(std::is_base_of<Base, A>::value, "");
};
(produce: an undefined class is not allowed as an argument to compiler intrinsic type trait)
类型'A'仍然不完全符合static_assert(根据这个概念的定义)。但是 - 编译器已经知道“类层次结构”并且可以为此提供答案。
当然 - 这个 static_assert 可以移动到析构函数或其他任何东西来解决这个问题,但在某些情况下它无法完成,例如:
class Base {};
template<typename T>
struct type_of {
static_assert(std::is_base_of<Base, T>::value, "T is not derived from Base");
using type = int; //* Some normal type in real use
};
class A : public Base {
public:
type_of<A>::type foo(); // Will not compile
};
应该不允许吗?
最佳答案
一个类定义在右大括号}
之后是完整的(即,一个类被认为已定义)。
在您的情况下,当您尝试将 A
与 std::is_base_of
一起使用时,A
尚未完全定义:
class A : public Base {
// no closing brace for A yet, thus A isn't fully defined here
static_assert(std::is_base_of<Base, A>::value, "");
};
另一方面,std::is_base_of
需要完全定义的类型才能工作。
因此错误。
作为一种解决方法,您可以将断言放在 A
的析构函数中:
class A : public Base {
~A() {
static_assert(std::is_base_of<Base, A>::value, "");
}
};
事实上,一个类类型在其成员函数体中被认为是完全定义的。
见 here更多细节(强调我的):
A class is considered a completely-defined object type ([basic.types]) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, noexcept-specifiers, and default member initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.
关于c++ - 为什么不能在类声明(不完整类型)中使用 "is_base_of"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48643562/
我试图断言模板参数将派生自某个基类。但是基类是通用的,在断言的上下文中,任何特化类型之间都没有区别。我如何断言模板参数派生自任何专用类型的泛型? 我试着把它写成 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
我是一名优秀的程序员,十分优秀!