Skip to content

Commit

Permalink
列表初始化通过禁止了从整数类型到浮点类型的转换,虽然完全不会有精度损失 https://zh.cppreference.com/w/cpp…
Browse files Browse the repository at this point in the history
…/language/list_initialization。

修正(虽然gcc 和 msvc 可以通过编译),显式类型转换,测试:https://godbolt.org/z/az9o3o9qe
  • Loading branch information
Mq-b committed Mar 16, 2024
1 parent ef7eac6 commit b105de0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions md/第一部分-基础知识/01函数模板.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ int main() {
template<typename...Args,typename RT = std::common_type_t<Args...>>
RT sum(const Args&...args) {
RT _[]{ args... };
RT _[]{ static_cast<RT>(args)... };
RT n{};
for (int i = 0; i < sizeof...(args); ++i) {
n += _[i];
Expand All @@ -594,7 +594,7 @@ RT sum(const Args&...args) {
}
int main() {
double ret = sum(1, 2, 3, 4, 5,6.7);
double ret = sum(1, 2, 3, 4, 5, 6.7);
std::cout << ret << '\n'; // 21.7
}
```
Expand Down

0 comments on commit b105de0

Please sign in to comment.