Topic: N1919 initializer list size and constexpr
Author: Edward Rosten <edward.rosten@gmail.com>
Date: Fri, 4 Sep 2009 12:44:00 CST Raw View
It is not clear from N1919 if the size of an initializer list is a
constexpr
>From 4.5:
"""
Clearly, there is enough information to deduce that s.size() is 3.
Equally clearly, making
s.size() a constant expression requires a special rule. The proposal
for generalizing
constant expressions (N1920=05-0180) suggests that this can be made to
work. We are
not sure whether this is really important or just something people
thought interesting. We
need a solid use case.
"""
Has there been any update on this? I would personally find static
sizing very useful. Also, what about nested lists? This would be
useful (for instance) for initializing a matrix, eg:
Matrix<3,3> m = {{1,2,3}, {4,5,6}, 7,8,9}};
template<int Rows, int Cols> struct Matrix{
Matrix(initializer_list<initializer_list<double>> l)
{
...
}
};
Is there any way of statically determining the size of the sub-
initializer lists? ie, in the constructor, would the result of m
[0].size() also be a constexpr? If initializer lists could be passed
around, then static verification of sizes could be done using code
such as:
... //In Matrix constructor
Verify<l[0].size, l[0].size()-1>::verify(l);
...
template<int Size, int N> struct Verify {
static void verify(initializer_list<initializer_list<double>> l)
{
static_assert(l[N].size() == Size);
Verify(Size, N-1)::verify(l);
}
};
template<int Size> struct Verify<Size, -1>{
static void verify(initializer_list<initializer_list<double>> l)
{}
};
-Ed
--
(You can't go wrong with psycho-rats.)(http://mi.eng.cam.ac.uk/~er258)
/d{def}def/f{/Times s selectfont}d/s{11}d/r{roll}d f 2/m{moveto}d -1
r 230 350 m 0 1 179{ 1 index show 88 rotate 4 mul 0 rmoveto}for/s 12
d f pop 235 420 translate 0 0 moveto 1 2 scale show showpage
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]