【发布时间】:2015-05-12 13:10:19
【问题描述】:
我有以下代码:
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,则正确的断言会失败,而另一个断言会成功。
附:最初我只有1 而不是static_cast<uint16_t>(1),并认为问题是由于数字文字1 具有int 类型但即使在此处显式转换后错误断言也会失败。
【问题讨论】:
-
另一个“不要在任何地方使用
auto”的例子。
标签: c++ c++11 auto integer-promotion