Re: how to boost::range_size work?
chuchelo@gmail.com writes:
I want to write code like this:
#include <vector>
#include <iostream>
Your program will require <ostream> as well.
#include <iterator>
template<std::size_t N>
std::vector<int> to_vector(const int i[N]) {
The problem is that N is merely a comment (and i a pointer to const
int). If you change the parameter type to a reference type, as in:
std::vector<int> to_vector(const int (&i)[N])
N becomes part of i's type, and the compiler will determine it from
the argument.
std::vector<int> v;
v.assign(i,i+N);
return v;
return std::vector<int>(i,i+N);
would be simpler. And obviously, if you write such a function, you'd
substitute a typename template parameter for int:
template<typename T, std::size_t N>
std::vector<T> to_vector(T const (&i)[N])
{
return std::vector<T>(i,i+N);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]