Skip to content

Commit ffdaf3a

Browse files
committed
add iterator_traits to ChildInstanstiablesIterator
I have seen the following compile error on g++-13 when trying to use std::find_if with ChildInstanstiablesIterator: [9/30] Building CXX object CMakeFiles/test_schema_node.dir/tests/schema_node.cpp.o FAILED: [code=1] CMakeFiles/test_schema_node.dir/tests/schema_node.cpp.o /usr/bin/g++-13 -DDOCTEST_CONFIG_SUPER_FAST_ASSERTS -I/home/tomas/zdrojaky/cesnet/libyang-cpp/include -I/home/tomas/zdrojaky/cesnet/libyang-cpp/build-gcc-asan -I/home/tomas/zdrojaky/cesnet/libyang-cpp/tests -I/home/tomas/zdrojaky/cesnet/libyang-cpp/src -I/home/tomas/zdrojaky/cesnet/libyang-cpp -isystem /home/tomas/zdrojaky/cesnet/build/prefixes/gcc-asan/include -Wall -Wextra -pedantic -Woverloaded-virtual -Wimplicit-fallthrough -Wsuggest-override -fsanitize=address,undefined -g -I/opt/boost1.88/include -std=gnu++23 -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT CMakeFiles/test_schema_node.dir/tests/schema_node.cpp.o -MF CMakeFiles/test_schema_node.dir/tests/schema_node.cpp.o.d -o CMakeFiles/test_schema_node.dir/tests/schema_node.cpp.o -c /home/tomas/zdrojaky/cesnet/libyang-cpp/tests/schema_node.cpp In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/string:51, from /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/stdexcept:39, from /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/system_error:43, from /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/fs_fwd.h:35, from /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/filesystem:48, from /home/tomas/zdrojaky/cesnet/libyang-cpp/include/libyang-cpp/Context.hpp:10, from /home/tomas/zdrojaky/cesnet/libyang-cpp/tests/schema_node.cpp:10: /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h: In instantiation of ‘constexpr _Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = libyang::ChildInstanstiablesIterator; _Predicate = __gnu_cxx::__ops::_Iter_pred<DOCTEST_ANON_FUNC_2()::<lambda(const libyang::SchemaNode&)> >]’: /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algo.h:3923:28: required from ‘constexpr _IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = libyang::ChildInstanstiablesIterator; _Predicate = DOCTEST_ANON_FUNC_2()::<lambda(const libyang::SchemaNode&)>]’ /home/tomas/zdrojaky/cesnet/libyang-cpp/tests/schema_node.cpp:269:17: required from here /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2117:23: error: no matching function for call to ‘__find_if(libyang::ChildInstanstiablesIterator&, libyang::ChildInstanstiablesIterator&, __gnu_cxx::__ops::_Iter_pred<DOCTEST_ANON_FUNC_2()::<lambda(const libyang::SchemaNode&)> >&, std::__iterator_traits<libyang::ChildInstanstiablesIterator, void>::iterator_category)’ 2117 | return __find_if(__first, __last, __pred, | ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ 2118 | std::__iterator_category(__first)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2052:5: note: candidate: ‘template<class _InputIterator, class _Predicate> constexpr _InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, input_iterator_tag)’ 2052 | __find_if(_InputIterator __first, _InputIterator __last, | ^~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2052:5: note: template argument deduction/substitution failed: /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2118:48: note: cannot convert ‘std::__iterator_category<libyang::ChildInstanstiablesIterator>(__first)’ (type ‘std::__iterator_traits<libyang::ChildInstanstiablesIterator, void>::iterator_category’ {aka ‘std::output_iterator_tag’}) to type ‘std::input_iterator_tag’ 2118 | std::__iterator_category(__first)); | ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2064:5: note: candidate: ‘template<class _RandomAccessIterator, class _Predicate> constexpr _RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, random_access_iterator_tag)’ 2064 | __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, | ^~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2064:5: note: template argument deduction/substitution failed: /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2118:48: note: cannot convert ‘std::__iterator_category<libyang::ChildInstanstiablesIterator>(__first)’ (type ‘std::__iterator_traits<libyang::ChildInstanstiablesIterator, void>::iterator_category’ {aka ‘std::output_iterator_tag’}) to type ‘std::random_access_iterator_tag’ 2118 | std::__iterator_category(__first)); | ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2115:5: note: candidate: ‘template<class _Iterator, class _Predicate> constexpr _Iterator std::__find_if(_Iterator, _Iterator, _Predicate)’ 2115 | __find_if(_Iterator __first, _Iterator __last, _Predicate __pred) | ^~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2115:5: note: template argument deduction/substitution failed: /usr/lib/gcc/x86_64-pc-linux-gnu/13.4.1/include/c++/bits/stl_algobase.h:2117:23: note: candidate expects 3 arguments, 4 provided 2117 | return __find_if(__first, __last, __pred, | ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ 2118 | std::__iterator_category(__first)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [21/30] Building CXX object CMakeFiles/test_data_node.dir/tests/data_node.cpp.o ninja: build stopped: subcommand failed. It seems our iterator category is wrongly deduced to std::output_iterator_tag which does not work with std::find_if. This patch adds the necessary typedefs to ChildInstanstiablesIterator to make it a valid input iterator. Interestingly, I have not seen this on my clang++-21, only on CI's g++-13. Change-Id: I0c4503cb3bd6166f3c79037ea2c4e3bed1520042
1 parent 4f2e502 commit ffdaf3a

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

include/libyang-cpp/ChildInstantiables.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: BSD-3-Clause
77
*/
88
#pragma once
9+
#include <iterator>
910
#include <libyang-cpp/SchemaNode.hpp>
1011
#include <libyang-cpp/export.h>
1112

@@ -16,6 +17,11 @@ class Module;
1617

1718
class LIBYANG_CPP_EXPORT ChildInstanstiablesIterator {
1819
public:
20+
using iterator_category = std::input_iterator_tag;
21+
using value_type = SchemaNode;
22+
using reference = void;
23+
using difference_type = std::ptrdiff_t;
24+
1925
friend ChildInstanstiables;
2026
SchemaNode operator*() const;
2127
ChildInstanstiablesIterator& operator++();

tests/schema_node.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ TEST_CASE("SchemaNode")
264264
}
265265

266266
REQUIRE(expectedPaths == actualPaths);
267+
268+
for (const auto& path : expectedPaths) {
269+
REQUIRE(std::find_if(children->begin(), children->end(), [&path](const libyang::SchemaNode& node) { return node.path() == path; }) != children->end());
270+
}
267271
}
268272

269273
DOCTEST_SUBCASE("unimplemented module")

0 commit comments

Comments
 (0)