In the MRE https://godbolt.org/z/jdjPzdGeo, is there a way to return an iterator for c
type arrays in Func
like what you see with std::array
in Func2
and Func3
? IDK what the return type would be.
Also, is there a way to make Func
constexpr like in Func2
?
Edit: Add the code here
#include <array>
std::pair<int*, std::size_t>
Func() noexcept
{
// why does constexpr instead of static not work?
static int arr[] = {1, 2};
return { arr, std::size(arr) };
}
constexpr std::pair<std::array<int,2>::const_iterator, std::array<int,2>::const_iterator>
Func2() noexcept
{
constexpr std::array<int, 2> arr {{1, 2}};
return { std::cbegin(arr), std::cend(arr) };
}
std::pair<std::array<int,2>::const_iterator, std::array<int,2>::const_iterator>
Func3() noexcept
{
static std::array<int, 2> arr {{1, 2}};
return { std::cbegin(arr), std::cend(arr) };
}
int main()
{
Func();
Func2();
Func3();
}
CodePudding user response:
Return iterator for c type arrays?
IDK what the return type would be.
The iterator type for arrays is a pointer. for example, if you have an array of int
, then the iterator type for the array is int*
.
and am expecting the return type to be an iterator of some sort.
int*
is an iterator.
int* works for std::begin but what about std::cbegin?
If you want a constant iterator, that would be const int*
.
Your Func2
returns dangling iterators as far as I can tell. Don't return iterators, references etc. to objects with automatic storage duration.