- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们在工作中的代码库中遇到了这种情况,我们就这是否是有效的 C++ 展开了激烈的争论。这是我能想出的最简单的代码示例:
template <class T>
class A {
public:
A() { subclass = static_cast<T*>(this); }
virtual void Foo() = 0;
protected:
T* subclass;
};
class C : public A<C> {
public:
C(int i) : i(i) { }
virtual void Foo() { subclass->Bar(); }
void Bar() { std::cout << "i is " << i << std::endl; }
private:
int i;
};
int main() {
C c(5);
c.Foo();
return 0;
}
这段代码在实践中 100% 有效(只要模板参数类型与子类类型匹配),但如果我们通过运行时分析器运行它,它会告诉我们 static_cast
无效,因为我们将 this
转换为 C*
但 C
构造函数尚未运行。果然,如果我们将 static_cast
更改为 dynamic_cast
,它返回 nullptr
并且此程序将在访问 i< 时失败并崩溃
在 Bar()
中。
我的直觉是,应该总是可以用 dynamic_cast
替换 static_cast
而不会破坏您的代码,这表明原始代码实际上取决于特定于编译器的 undefined行为。然而,on cppreference它说:
If the object expression refers or points to is actually a base class subobject of an object of type D, the result refers to the enclosing object of type D.
问题是,在D
类型的对象构造完成之前,它是D
类型对象的基类子对象吗?或者这是未定义的行为?我的 C++ 规则律师水平不足以解决这个问题。
最佳答案
在我看来,根据标准的当前措辞,这是明确定义的:C
对象在 static_cast
时存在,尽管它正在 build 中并且其生命周期尚未开始。这似乎使 static_cast
根据 [expr.static.cast]/11 定义良好,部分内容如下:
... If the prvalue of type “pointer to cv1
B
” points to aB
that is actually a base class subobject of an object of typeD
, the resulting pointer points to the enclosing object of typeD
. Otherwise, the behavior is undefined.
它并没有说 D
对象的生命周期必须已经开始。
我们可能还想查看有关执行从派生到基的隐式转换何时合法的明确规则,[class.cdtor]/3 :
To explicitly or implicitly convert a pointer (a glvalue) referring to an object of class
X
to a pointer (reference) to a direct or indirect base classB
ofX
, the construction ofX
and the construction of all of its direct or indirect bases that directly or indirectly derive fromB
shall have started and the destruction of these classes shall not have completed, otherwise the conversion results in undefined behavior. To form a pointer to (or access the value of) a direct non-static member of an objectobj
, the construction ofobj
shall have started and its destruction shall not have completed, otherwise the computation of the pointer value (or accessing the member value) results in undefined behavior.
根据这个规则,一旦编译器开始构造基类A<C>
, 从 C*
隐式转换是明确定义的至 A<C>*
.在那之前,它会导致 UB。其原因基本上与虚拟基类有关:如果 A<C>
的路径由 C
继承包含任何虚拟继承,转换可能依赖于链中构造函数之一设置的数据。对于从base到derived的转换,如果链上确实存在虚拟继承,static_cast
不会编译,所以我们真的不需要问自己这个问题,但是这些数据是否足以让我们走另一条路?
对于 static_cast
,我真的在标准文本中看不到任何内容,也看不到任何理由在您的示例中没有明确定义,在 static_cast
的任何其他情况下也没有当允许反向隐式转换(或 static_cast
)时从 base 到 derived (虚拟继承的情况除外,正如我之前所说,无论如何都会导致编译错误)。
(如果更早执行此操作是否定义明确?在大多数情况下这是不可能的;在从 static_cast
转换之前,您怎么可能尝试将 B*
从 D*
转换为 D*
到 B*
是允许的,而没有通过执行后者精确地获得 B*
指针?如果答案是你从 D*
到 B*
通过中间基类 C1
其构造函数已启动,但是有另一个中间基类 C2
共享相同的 B
基类子对象并且它的构造尚未开始,那么 B
是一个虚拟基类,同样,这意味着编译器将阻止您尝试从 static_cast
到 B*
回到 D*
。所以我认为这里没有任何问题需要解决。)
关于c++ - 您可以将 "this"静态转换为基类构造函数中的派生类,然后稍后使用结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73172193/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!