gpt4 book ai didi

c++ - 转换常量表达式定义的澄清

转载 作者:太空狗 更新时间:2023-10-29 20:56:16 26 4
gpt4 key购买 nike

(在 my recent questionanother question 之后。)

[expr.const]/4 表示:

A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only

  • ... (list omitted)

and where the reference binding (if any) binds directly.

(强调我的。)

这里有两点不是很明白。

首先,“转换后的表达式”(强调)指的是哪个表达式?

举个例子

class A; // assume it can be implicitly converted to `int`
A foo(); // or maybe constexpr
template<int n> void bar();

现在,如果我写

bar<foo()>();

那么哪个表达式应该是常量表达式呢?应整foo()表达式是常量,或者类似于 static_cast<int>(foo())

据我了解 my recent question ,只有后者需要保持不变。这是真的吗?

其次,“引用绑定(bind)”指的是整个过程的哪个阶段?它仅指模板参数本身是引用( template<int& x>... )的情况吗?或者它是否要求在类型转换或转换表达式的评估期间某处发生的任何引用绑定(bind)应该是直接的?还是指未转换的表达式是引用本身 ( A& a=...; bar<a>(); ) 的情况?

最佳答案

First, which expression does "the converted expression" (emphased) refer to?

隐式转换引入了相应初始化的临时对象:

T e = /* original expression */;

e 是“转换后的表达式”。 T = int 在你的情况下。

then which expression should be constant expression?

e

此外,foo 隐式调用的转换运算符函数必须是 constexpr 函数,符合 [expr.const]/(2.2) .

Second, what stage of the whole process does "reference binding" refer to?

T 是引用类型时,该引用 - 上面示例中的 e - 应直接绑定(bind)。在表达式内部或感兴趣之前不需要绑定(bind)。

关于c++ - 转换常量表达式定义的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com