我有以下代码:
uint16_t getLastMarker(const std::string &number);
...
const auto msgMarker = getLastMarker(msg.number) + static_cast<uint16_t>(1);
static_assert(std::is_same<decltype(msgMarker), const int>::value, "Should fail");
static_assert(std::is_same<decltype(msgMarker), const uint16_t>::value, "Should not fail");
我希望第一个断言会失败,而第二个断言不会。然而gcc 4.9.2
,clang 3.6
反其道而行之。如果我在代码中使用 uint16_t 而不是 auto ,则正确的断言会失败,而另一个断言会成功。
PS 最初我只是1
代替static_cast<uint16_t>(1)
并认为问题是由于数字文字1
的类型为 int 但即使在此处显式转换后错误断言也会失败。