- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
MSVC 和 clang/gcc 对于是否可以在三元运算符中使用两个不同的整型常量(以及它们是否具有 common_type
)存在分歧:
#include <utility>
int main()
{
return false
? std::integral_constant<int, 1>()
: std::integral_constant<int, 2>();
}
上面的代码片段在 clang 和 gcc 中编译得很好,但在 MSVC 中却不行。根据标准,正确的行为是什么?如果是 clang/gcc 行为,那么用于推断这两种不同类型的公共(public)类型的转换序列是什么?
最佳答案
tldr;代码格式正确。条件表达式的类型为 int
和值(value)2
.这是一个 MSVC 错误。
来自 [expr.cond]:
Otherwise, if the second and third operand have different types and either has (possibly cv-qualified) class type, or [...], an attempt is made to form an implicit conversion sequence (13.3.3.1) from each of those operands to the type of the other. [...] Attempts are made to form an implicit conversion sequence from an operand expression
E1
of typeT1
to a target type related to the typeT2
of the operand expressionE2
as follows: [...]
— If E2 is an lvalue, [...]
— If E2 is an xvalue, [...]
— If E2 is a prvalue or if neither of the conversion sequences above can be formed and at least one of the operands has (possibly cv-qualified) class type:
— if T1 and T2 are the same class type (ignoring cv-qualification), or one is a base class of the other, and T2 is at least as cv-qualified as T1, the target type is T2,
— otherwise, the target type is the type that E2 would have after applying the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions.
所以我们试图从类型 std::integral_constant<int, 1>
形成一个隐式转换序列输入 std::integral_constant<int, 2>
.那是不可行的。反向的隐式转换序列也不可行。这些类型根本无法相互转换。
所以我们继续:
If no conversion sequence can be formed, the operands are left unchanged and further checking is performed as described below. [...]
If the second and third operands are glvalues of the same value category and have the same type, [...]
Otherwise, the result is a prvalue. If the second and third operands do not have the same type, and either has (possibly cv-qualified) class type, overload resolution is used to determine the conversions (if any) to be applied to the operands (13.3.1.2, 13.6). If the overload resolution fails, the program is ill-formed.
好的,我们可以执行什么重载决议?来自 [over.match.oper]:
If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator.
在 [over.built] 中将内置函数指定为:
For every pair of promoted arithmetic types L and R, there exist candidate operator functions of the form
LR operator?:(bool, L , R );
where LR is the result of the usual arithmetic conversions between types L and R.
其中一个内置函数是 int operator?:(bool, int, int)
.自 std::integral_constant<int, V>
确实有一个 operator int()
,这是两个参数的可行转换。
我们在 [expr.cond] 中继续:
Otherwise, the conversions thus determined are applied, and the converted operands are used in place of the original operands for the remainder of this section.
Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the second and third operands. After those conversions, one of the following shall hold:
— The second and third operands have the same type; the result is of that type and the result object is initialized using the selected operand.
此时,第二个和第三个操作数确实具有相同的类型:int
.所以结果对象被初始化为 int
, 并且表达式是合式的。
关于c++ - 三元运算符中的 integral_constants,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42421458/
MSVC 和 clang/gcc 对于是否可以在三元运算符中使用两个不同的整型常量(以及它们是否具有 common_type)存在分歧: #include int main() { retu
我想将传递给函数的编译时间常量整数捕获为模板类型。 future 的目标是推出我自己的(非常有限的)表达式模板,该模板从非常简单的表达式(编译时常量 int 和 in 变量的总和)创建表达式,例如:-
我刚刚进入元编程,正在 youtube 上观看 Cppcon channel ,并看到了这个 std::integral_constant ,但找不到它的用途。 据我所知,这是一种将值与其类型一起“打
您能否解释一下,为什么以下示例中的 integral_constant 和 constexpr 方法会导致不同的行为? #include using namespace std; struct Lo
利用Scott Schurr's str_const我有一个 constexpr 字符串。 class StrConst { public: template constexpr St
它的实际用例是什么? std::integral_constant 我可以理解这是一个值为 2 的包装器: typedef std::integral_constant two_t 但为什么不直接使用
考虑这段代码 #include #include enum Thing {Thing0, Thing1, Thing2, NumThings}; enum Object {Object0, Obj
在C++标准库中,有没有对integral_constant进行 bool 运算的模板类型? (其中 X 是 true 或 false )? 作为一个简单的例子,您有两个这样的函数重载: void f
昨天我读了一个blog entry关于将编译时已知函数参数从 constexpr 函数转换为类似 std::integral_constant<> 的类型. 一个可能的用法示例是从用户定义的文字转换类
我想创建一个 std::integral_constant 的“容器版本”。这是一个用元素类型和非类型参数参数化的类型: enum class A { a = 1 struct static
很抱歉问了这么简单的问题,但我无法轻易找到答案。 Google 对“C++ 否定 integral_constant”和类似查询没有任何有趣的说法。 在 C++11 中是否有任何特征使 std::tr
查看 std::is_same 的实现我们可以看到一些内部函数(继承自 integral_constant )。为了方便起见,让我复制 g++ 代码: template struct inte
我有几种类型,我想“绑定(bind)”一个 std::integral_constant编译时每种类型的顺序 ID 值。 例子: struct Type00 { }; struct Type01 {
我设法转换了一个 template struct List到 integral_constant 的列表但是,是否可以从 integral_constant 转换为回到int是吗? 以下是我如何从 I
#include #include int main(){ //creating an integral constant with constexpr constexpr uns
我想从 char* 和“kernel32.dll”中获取整数常量,但总是失败。以下是我失败的尝试,谁能告诉我正确的用法? error 1: cout ::value ::value ::value (
在C++14标准中,std::integral_constant模板定义如下: template struct integral_constant { static constexpr T va
#include #include #include #include template auto make_index_dispatcher(std::index_sequence) {
假设我有一个 hana::integral_constant 的元组,如下所示: auto tuple_of_int_const = hana::make_tuple(hana::integral_c
从 C++20 开始可以使用 auto实现整数常量的模板参数: Try it online! template struct integral_constant2 : std::integr
我是一名优秀的程序员,十分优秀!