以下cpp表达式中的优先顺序是什么?

问题描述 投票:-1回答:2
uint64_t author.value = 0;
uint64_t timestamp    = 1577369253;    
uint128_t skey = static_cast<uint128_t>(author.value) << 64 | timestamp;

skey的价值是什么?无论时间戳的值如何,我都将其设置为零。可能是什么原因?

c++ casting operator-precedence
2个回答
1
投票
uint128_t skey = ( static_cast<uint128_t>(author.value) << 64 ) | timestamp;

优先级

Parenthesized expression ( expr ) Lambda [ capture-list ] lambda-declarator { stmt-List } Scope resolution class-name :: member Scope resolution namespace-name :: member Global :: name Each box holds operators with the same precedence. Operators in higher boxes have higher precedence. For example, N::x.m means (N::m).m rather than the illegal N::(x.m).256 Expressions Member selection object . member Member selection pointer −> member Subscripting pointer [ expr ] Function call expr ( expr-list ) Value construction type { expr-list } §11.3.2 Function-style type conversion type ( expr-list ) Post increment lvalue ++ Post decrement lvalue −− Type identification typeid ( type ) Run-time type identification typeid ( expr ) Run-time checked conversion dynamic_cast < type > ( expr ) Compile-time checked conversion static_cast < type > ( expr ) §11.5.2 Unchecked conversion reinterpret_cast < type > ( expr ) §11.5.2 const conversion const_cast < type > ( expr ) §11.5.2 Size of object sizeof expr §6.2.8 Size of type sizeof ( type ) §6.2.8 Size of parameter pack sizeof... name §28.6.2 Alignment of type alignof ( type ) §6.2.9 Pre increment ++ lvalue §11.1.4 Pre decrement −− lvalue §11.1.4 Complement ˜ expr §11.1.2 Not ! expr Unary minus − expr Unary plus + expr Address of & lvalue Dereference ∗ expr Create (allocate) new type §11.2 Create (allocate and initialize) new type ( expr-list ) §11.2 Create (allocate and initialize) new type { expr-list } §11.2 Create (place) new ( expr-list ) type §11.2.4 Create (place and initialize) new ( expr-list ) type ( expr-list ) §11.2.4 Create (place and initialize) new ( expr-list ) type { expr-list } §11.2.4 Destroy (deallocate) delete pointer §11.2 Destroy array delete [] pointer §11.2.2 Can expression throw? noexcept ( expr ) §13.5.1.2 Cast (type conversion) ( type ) expr §11.5.3 Member selection object .∗ pointer-to-member §20.6 Member selection pointer −>∗ pointer-to-member §20.6 For example, postfix ++ has higher precedence than unary ∗, so ∗p++ means ∗(p++), not (∗p)++. Multiply expr ∗ expr §10.2.1 Divide expr / expr §10.2.1 Modulo (remainder) expr % expr §10.2.1 Add (plus) expr + expr §10.2.1 Subtract (minus) expr − expr §10.2.1 Shift left expr << expr §11.1.2 Shift right expr >> expr §11.1.2 Less than expr < expr §2.2.2 Less than or equal expr <= expr §2.2.2 Greater than expr > expr §2.2.2 Greater than or equal expr >= expr §2.2.2 Equal expr == expr §2.2.2 Not equal expr != expr §2.2.2 Bitwise and expr & expr §11.1.2 Bitwise exclusive-or expr ˆ expr §11.1.2 Bitwise inclusive-or expr | expr §11.1.2 Logical and expr && expr §11.1.1 Logical inclusive or expr || expr §11.1.1 Conditional expression expr ? expr : expr List { expr-list } Throw exception throw expr Simple assignment lvalue = expr §10.2.1 Multiply and assign lvalue ∗= expr §10.2.1 Divide and assign lvalue /= expr §10.2.1 Modulo and assign lvalue %= expr §10.2.1 Add and assign lvalue += expr Subtract and assign lvalue −= expr Shift left and assign lvalue <<= expr Shift right and assign lvalue >>= expr Bitwise and and assign lvalue &= expr Bitwise inclusive-or and assign lvalue |= expr Bitwise exclusive-or and assign lvalue ˆ= expr comma (sequencing) expr , expr


-1
投票
uint128_t skey = (static_cast<uint128_t>(author.value) << 64) | timestamp;

并且据此,按位移位(7)的优先级高于按位或(13):https://en.cppreference.com/w/cpp/language/operator_precedence

© www.soinside.com 2019 - 2024. All rights reserved.