refactor: Specify const in std::span constructor, where needed

The std::span constructor requires std::ranges::borrowed_range, which
tries to protect against dangling references.

One way to disable the check is to specify the std::span's element type
as const in the constructor call.

Otherwise, a compile error will look like:

 include/c++/span: note: candidate constructor not viable: no known conversion from 'std::vector<unsigned char>' to 'const span<unsigned char>' for 1st argument
      |       span(const span&) noexcept = default;
      |       ^    ~~~~~~~~~~~
 ...
 include/c++/span: note: candidate template ignored: constraints not satisfied [with _Range = std::vector<unsigned char>]
      |         span(_Range&& __range)
      |         ^
 include/c++/span: note: because 'std::vector<unsigned char>' does not satisfy 'borrowed_range'
      |           && (ranges::borrowed_range<_Range> || is_const_v<element_type>)
      |               ^
 include/c++/bits/ranges_base.h: note: because 'std::vector<unsigned char>' does not satisfy '__maybe_borrowed_range'
      |       = range<_Tp> && __detail::__maybe_borrowed_range<_Tp>;
      |                       ^
 include/c++/bits/ranges_base.h: note: because 'is_lvalue_reference_v<std::vector<unsigned char> >' evaluated to false
      |         = is_lvalue_reference_v<_Tp>
      |           ^
 include/c++/bits/ranges_base.h: note: and 'enable_borrowed_range<remove_cvref_t<vector<unsigned char, allocator<unsigned char> > > >' evaluated to false
      |           || enable_borrowed_range<remove_cvref_t<_Tp>>;
      |              ^
 include/c++/span: note: and 'is_const_v<element_type>' evaluated to false
      |           && (ranges::borrowed_range<_Range> || is_const_v<element_type>)
      |                                                 ^
This commit is contained in:
MarcoFalke 2024-12-17 22:33:31 +01:00
parent faaf4800aa
commit fa494a1d53
No known key found for this signature in database
2 changed files with 2 additions and 3 deletions

View file

@ -1700,9 +1700,8 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
BOOST_CHECK_EQUAL(HexStr(sighash), input["intermediary"]["sigHash"].get_str());
// To verify the sigmsg, hash the expected sigmsg, and compare it with the (expected) sighash.
BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << Span{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << std::span<const uint8_t>{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
}
}
}

View file

@ -304,7 +304,7 @@ public:
if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
s << m_base_data;
} else {
s << Span{HexStr(Span{&m_base_data, 1})};
s << std::span<const char>{HexStr(Span{&m_base_data, 1})};
}
}