Topic: n1919
Author: Thorsten Ottosen <thorsten.ottosen@dezide.com>
Date: Tue, 31 Jan 2006 13:29:06 CST Raw View
Vaughn Cato wrote:
> I recently read Bjarne Stroustrup and Gabriel Dos Reis's paper on
> generalized initializer lists. One point that I don't understand is
> 4.5.3. Here they say that having a T(&)[N] constructor wouldn't be
> good because it would force all initializers to be templates, and that
> treating the initializer as a constant would not be much of an
> advantage. While I agree with these points, it seems there is an easy
> solution.
>
> template <typename T>
> class initializer_list {
> public:
> template <size_t N>
> initializer_list(const T (&init)[N])
> : begin_(init),
> end_(init+N)
these two function can of course be library function too.
> {
> }
> private:
> const T * begin_;
> const T * end_;
> };
>
> Then you could have
>
> template <typename T>
> class Array {
> public:
> Array(const initializer_list<T> & init);
> .
> .
> .
> };
>
> void f()
> {
> A a({1,2,3,4});
> }
>
>
> This would make the initializer_list template be a pure library feature
> and would allow you to still make use of the static size of the
> initializer when it might be more efficient or desired.
>
> Any thoughts?
It's a good idea.
-Thorsten
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Vaughn Cato" <vcato@bellsouth.net>
Date: Tue, 10 Jan 2006 10:03:51 CST Raw View
I recently read Bjarne Stroustrup and Gabriel Dos Reis's paper on
generalized initializer lists. One point that I don't understand is
4.5.3. Here they say that having a T(&)[N] constructor wouldn't be
good because it would force all initializers to be templates, and that
treating the initializer as a constant would not be much of an
advantage. While I agree with these points, it seems there is an easy
solution.
template <typename T>
class initializer_list {
public:
template <size_t N>
initializer_list(const T (&init)[N])
: begin_(init),
end_(init+N)
{
}
private:
const T * begin_;
const T * end_;
};
Then you could have
template <typename T>
class Array {
public:
Array(const initializer_list<T> & init);
.
.
.
};
void f()
{
A a({1,2,3,4});
}
This would make the initializer_list template be a pure library feature
and would allow you to still make use of the static size of the
initializer when it might be more efficient or desired.
Any thoughts?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]