Closed Flamefire closed 5 years ago
How is arraySize
known at compile-time?
For now I leave 1. unanswered.
#include "nonstd/span.hpp"
#include <iostream>
using nonstd::span;
// current span lite default: std::ptrdiff_t:
using index_type = span<int>::index_type;
int memory[] =
{
1,2,3,
4,5,6,
7,8,9,
0,0,0,
0,0,0
};
size_t numArrays()
{
return 4;
}
template< typename T, index_type Extent >
void consume( int i, span<T,Extent> spn )
{
std::cout << i << ": ";
for( auto x : spn )
{
std::cout << x << ", ";
}
std::cout << "\n";
}
template< typename T >
void consume( span<T> spn, size_t numArrays )
{
constexpr index_type arraySize = 3; // <== how is this known at compile-time? <==
for( int i = 0; i < numArrays; i++ )
{
consume( i, span<T,arraySize>( spn.begin() + i * arraySize, arraySize ) );
}
}
int main()
{
consume( span<int>( memory ), numArrays() );
}
// g++ -std=c++17 -Dspan_CONFIG_INDEX_TYPE=size_t -I../include -o issue-31.exe issue-31.cpp && issue-31.exe
// g++ -std=c++17 -Dspan_CONFIG_INDEX_TYPE=std::ptrdiff_t -I../include -o issue-31.exe issue-31.cpp && issue-31.exe
// > g++ -std=c++17 -I../include -o issue-31.exe issue-31.cpp && issue-31.exe
// 0: 1, 2, 3,
// 1: 4, 5, 6,
// 2: 7, 8, 9,
// 3: 0, 0, 0,
Thank you.
How is arraySize known at compile-time?
In our case the sub-array is an image with fixed size. We can get any number of such images but each has the same size.
Ok, I was hoping for something less repetitive than span<T,arraySize>( spn.begin() + i * arraySize, arraySize )
(mentioning the type again and arraySize 3 times) And is there a reason for using begin
over data
? I'd thought that this would be invalid as begin
is an iterator (and only happens to be a pointer)
Compile-time over run-time computations.
Can you elaborate a bit? I'm highly interested in which cases this might be beneficial (in this case). So what computations are then performed on compiletime?
Indeed, use data()
.
In C++17 one can avoid one use of arraySize
:
consume( i, span( spn.data() + i * arraySize, arraySize ) );
Compile-time over run-time computations.
was a generic answer.
Depends on what you're going to do and it's perhaps easier to investigate yourself as you know your code, as opposed to multiple back and forths...
Just a question on preferred usage: Assume I have a memory which contains a number of arrays of fixed size. I get this memory as a
span<T>
and the array count as a runtime parameter. Hence the span size is a runtime parameter. Now I want to Process this memory chunk 1 array at a time:Although
arraySize
is a compile time constant and could be filled into the subArray template param, subspan does not do/allow that.Questions: