Skip to content

Commit fa494a1

Browse files
author
MarcoFalke
committed
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>) | ^
1 parent faaf480 commit fa494a1

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

src/test/script_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,9 +1700,8 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
17001700
BOOST_CHECK_EQUAL(HexStr(sighash), input["intermediary"]["sigHash"].get_str());
17011701

17021702
// To verify the sigmsg, hash the expected sigmsg, and compare it with the (expected) sighash.
1703-
BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << Span{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
1703+
BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << std::span<const uint8_t>{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
17041704
}
1705-
17061705
}
17071706
}
17081707

src/test/serialize_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class Base
304304
if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
305305
s << m_base_data;
306306
} else {
307-
s << Span{HexStr(Span{&m_base_data, 1})};
307+
s << std::span<const char>{HexStr(Span{&m_base_data, 1})};
308308
}
309309
}
310310

0 commit comments

Comments
 (0)