For a homework assignment, I'm trying to create my own versions of std::find
, std::begin
, std::end
, and std::size
.
I've written some code that looks like this:
#include <vector>
template <typename I, typename T>
I find(const I& beg, const I& end, const T& sought)
{/* ... */}
template <typename T, size_t S>
T* begin(T (&a)[S])
{return &a;}
template <typename T, size_t S>
T* end(T (&a)[S])
{return &a + S;}
template <typename T, size_t S>
constexpr size_t size(T (&)[S])
{return S;}
int main()
{
std::vector<int> vec = {0, 1, 2, 3};
// ...
// test not-found case (and `size()`)
find(begin(vec), end(vec), size(vec));
// ...
return 0;
}
(size_t
should implicitly convert to int
)
However when I compile, clang gives off the following error:
$ clang++ -o program ./*.cpp -std=c++11 -Wall -Wextra -Wpedantic -Wconversion -Wnon-virtual-dtor
./main.cpp:##:##: error: no matching function for call to 'size'
find(begin(vec), end(vec), size(vec)
^~~~
./main.cpp:##:##: note: candidate template ignored: could not match 'T [S]' against 'std::vector<int>'
constexpr size_t size(T (&)[S])
^
1 error generated.
What really baffles me is that both size()
and begin()
/end()
have the exact same template structure and parameter list, and yet the former throws a compile error while the latters don't when given the same input.
I don't think the different return type is the issue, since if it was find()
would be complaining, not size()
.
So how is size()
different from the other functions that it would throw this error with the same template structure, parameter list, and input?