
On Sun, Jan 13, 2013 at 2:39 PM, Kyle Lutz <kyle.r.lutz@gmail.com> wrote:
On Sun, Jan 13, 2013 at 11:23 AM, Thomas Heller <thom.heller@gmail.com>
wrote:
On Sunday, January 13, 2013 14:36:50 Mathias Gaunard wrote:
On 13/01/2013 10:38, Thomas Heller wrote:
The C++11 code looks too complicated. Wouldn't https://gist.github.com/4522812 be enough?
Indeed it is! I've updated the C++11 code to use the simpler initialization method. Thanks!
I don't think it is a good idea that make_array(1, 1., 1.f) makes an array of int. It seems quite arbitrary that the first argument would decide the
type.
Right. That is another problem. I just tried to simplify the implementation of the original proposal.
As far as type-deduction, how would you guys feel about an implementation using boost::common_type? Something along the lines of:
// make_array with deduced type template<class... Args> inline array<typename common_type<Args...>::type, sizeof...(Args)> make_array(Args&&... args) { typedef typename common_type<Args...>::type T;
return array<T, sizeof...(Args)> {{ static_cast<T>(std::forward<Args>(args))... }}; }
So that make_array(1, 2.1f) would return array<float, 2> and make_array(1, 2.1f, 3.2) would return array<double, 3>.
Thoughts?
Thanks, Kyle
Thanks for the feedback everyone! I've updated the make_array() function to use boost::common_type<> to do the type-deduction instead of always using the type of the first argument. Furthermore, explicitly specifying the array's value type is still supported. To better illustrate, here's a set of make_array() invocations along with the return type: make_array(1) => array<int, 1> make_array<float>(1) => array<float, 1> make_array(1, 1.2f) => array<float, 2> make_array<int>(1, 1.2f) => array<int, 2> make_array(1, 1.2f, 3.2) => array<double, 3> Thoughts/comments? Cheers, Kyle