Closed GiulioRomualdi closed 2 years ago
with this
std::vector<int32_t> v{2};
matioCpp::Span<int32_t> s{v};
everything works fine
It is nice to have this deduction guide since it will allow to write the following code
void foo(Span<int> s);
std::vector<int> v{1};
foo(v);
right now what we should do is
void foo(Span<int> s);
std::vector<int> v{1};
foo(make_span(v));
Notice that make_span
is not defined by the standard
Yes, the relevant error is
In file included from /home/gromualdi/robot-code/yarp-telemetry/src/libYARP_telemetry/src/yarp/telemetry/experimental/Record.h:12,
from /home/gromualdi/robot-code/yarp-telemetry/src/examples/circular_buffer_record_example.cpp:14:
/home/gromualdi/robot-code/robotology-superbuild/build/install/include/matioCpp/Span.h:411:24: note: candidate: ‘template<class ElementType, long int Extent, class Container, class, class> Span(const Container&)-> matioCpp::Span<ElementType, Extent>’
411 | MATIOCPP_CONSTEXPR Span(const Container& cont) : Span(cont.data(), static_cast<index_type>(cont.size()))
| ^~~~
/home/gromualdi/robot-code/robotology-superbuild/build/install/include/matioCpp/Span.h:411:24: note: template argument deduction/substitution failed:
/home/gromualdi/robot-code/yarp-telemetry/src/examples/circular_buffer_record_example.cpp:44:27: note: couldn’t deduce template parameter ‘ElementType’
44 | matioCpp::Span s{v};
| ^
In file included from /home/gromualdi/robot-code/yarp-telemetry/src/libYARP_telemetry/src/yarp/telemetry/experimental/Record.h:12,
from /home/gromualdi/robot-code/yarp-telemetry/src/examples/circular_buffer_record_example.cpp:14:
/home/gromualdi/robot-code/robotology-superbuild/build/install/include/matioCpp/Span.h:402:24: note: candidate: ‘template<class ElementType, long int Extent, class Container, class, class> Span(Container&)-> matioCpp::Span<ElementType, Extent>’
402 | MATIOCPP_CONSTEXPR Span(Container& cont) : Span(cont.data(), static_cast<index_type>(cont.size()))
| ^~~~
/home/gromualdi/robot-code/robotology-superbuild/build/install/include/matioCpp/Span.h:402:24: note: template argument deduction/substitution failed:
/home/gromualdi/robot-code/yarp-telemetry/src/examples/circular_buffer_record_example.cpp:44:27: note: couldn’t deduce template parameter ‘ElementType’
As you noticed, it fails to deduce the ElementType
.
I am not sure I understood what you meant in
In this case the used deduction guides used should be:
template<class R> span(R&&) -> span<std::remove_reference_t<std::ranges::range_reference_t<R>>>;
I am not sure I understood what you meant in
In this case the used deduction guides used should be:
template<class R> span(R&&) -> span<std::remove_reference_t<std::ranges::range_reference_t<R>>>;
Accordingly to what is written here
the deduction guide used for std::vector
is
std::ranges::range_reference_t
Ah I see. The problem is that std::ranges
is C++20 (https://en.cppreference.com/w/cpp/ranges)
This implementation seems able to perform the deduction. https://github.com/martinmoene/span-lite
What do you think to port it matio-cpp?
Before taking any decision let me try in a toy problem 😃
The following code works with span-line
#include "nonstd/span.hpp"
#include <array>
#include <vector>
#include <iostream>
std::ptrdiff_t size( nonstd::span<const int> spn )
{
return spn.size();
}
int main()
{
int arr[] = { 1, };
std::cout <<
"C-array:" << size( arr ) <<
" array:" << size( std::array <int, 2>{ 1, 2, } ) <<
" vector:" << size( std::vector<int >{ 1, 2, 3, } );
}
I would say that before moving to a completely new implementation, I would like to understand if it is possible to fix the current implementation.
Moving to a new implementation of span might be potentially a lot of work (also for those applications depending on this). I would rather do this step only once, when std::span
will be more accessible
I tried to edit the SpanUnitTest
as follows
But I am able to compile :thinking:
Accordingly to the
std
documentation the following code should workHowever when I try to compile the code I got
In this case the used deduction guides used should be: