From fa494a1d53f3f030fafe7b533d72b2200428a0fd Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 17 Dec 2024 22:33:31 +0100 Subject: [PATCH] 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' to 'const span' for 1st argument | span(const span&) noexcept = default; | ^ ~~~~~~~~~~~ ... include/c++/span: note: candidate template ignored: constraints not satisfied [with _Range = std::vector] | span(_Range&& __range) | ^ include/c++/span: note: because 'std::vector' does not satisfy 'borrowed_range' | && (ranges::borrowed_range<_Range> || is_const_v) | ^ include/c++/bits/ranges_base.h: note: because 'std::vector' 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 >' evaluated to false | = is_lvalue_reference_v<_Tp> | ^ include/c++/bits/ranges_base.h: note: and 'enable_borrowed_range > > >' evaluated to false | || enable_borrowed_range>; | ^ include/c++/span: note: and 'is_const_v' evaluated to false | && (ranges::borrowed_range<_Range> || is_const_v) | ^ --- src/test/script_tests.cpp | 3 +-- src/test/serialize_tests.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index 59eb90bd27a..8753ddee37f 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -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{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str()); } - } } diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index b28e1b41966..7e403694e22 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -304,7 +304,7 @@ public: if (s.template GetParams().m_base_format == BaseFormat::RAW) { s << m_base_data; } else { - s << Span{HexStr(Span{&m_base_data, 1})}; + s << std::span{HexStr(Span{&m_base_data, 1})}; } }