Skip to content

Commit 0ac5ca6

Browse files
Add a static_cast in args_t::size to silence a conversion warning (#966)
* Add a static_cast in `args_t::size` to silence a conversion warning Signed-off-by: Neil Henderson <[email protected]> * Change `static_cast` to `unsafe_narrow` I know, but negative numbers... --------- Signed-off-by: Neil Henderson <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent b24996f commit 0ac5ca6

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

include/cpp2util.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,19 @@ class finally_presuccess
17271727
};
17281728

17291729

1730+
//-----------------------------------------------------------------------
1731+
//
1732+
// An implementation of GSL's narrow_cast with a clearly 'unsafe' name
1733+
//
1734+
//-----------------------------------------------------------------------
1735+
//
1736+
template <typename C, typename X>
1737+
constexpr auto unsafe_narrow( X&& x ) noexcept -> decltype(auto)
1738+
{
1739+
return static_cast<C>(CPP2_FORWARD(x));
1740+
}
1741+
1742+
17301743
//-----------------------------------------------------------------------
17311744
//
17321745
// args: see main() arguments as a container of string_views
@@ -1771,7 +1784,7 @@ struct args_t
17711784
auto end() const -> iterator { return iterator{ argc, argv, argc }; }
17721785
auto cbegin() const -> iterator { return begin(); }
17731786
auto cend() const -> iterator { return end(); }
1774-
auto size() const -> std::size_t { return argc; }
1787+
auto size() const -> std::size_t { return cpp2::unsafe_narrow<std::size_t>(argc); }
17751788
auto ssize() const -> int { return argc; }
17761789

17771790
auto operator[](int i) const {
@@ -1800,19 +1813,6 @@ template<typename T>
18001813
using alien_memory = T volatile;
18011814

18021815

1803-
//-----------------------------------------------------------------------
1804-
//
1805-
// An implementation of GSL's narrow_cast with a clearly 'unsafe' name
1806-
//
1807-
//-----------------------------------------------------------------------
1808-
//
1809-
template <typename C, typename X>
1810-
constexpr auto unsafe_narrow( X&& x ) noexcept -> decltype(auto)
1811-
{
1812-
return static_cast<C>(CPP2_FORWARD(x));
1813-
}
1814-
1815-
18161816
//-----------------------------------------------------------------------
18171817
//
18181818
// has_flags: query whether a flag_enum value has all flags in 'flags' set

0 commit comments

Comments
 (0)