Topic: Is it safe to cast std::array::data() to built-in array reference?
Author: "Niels Dekker - no reply address" <invalid@this.is.invalid>
Date: Tue, 1 Dec 2009 16:38:12 CST Raw View
Can someone please tell me if the following function,
user_namespace::get_c_array, is guaranteed to get a valid reference to a
built-in array from a non-empty C++0x std::array?
namespace user_namespace
{
template <typename T, std::size_t N>
T(& get_c_array(std::array<T,N>& arg) )[N]
{
return *static_cast<T(*)[N]>(
static_cast<void*>(arg.data())
);
}
}
I'm asking because a related issue that I submitted was recently rejected:
LWG #930, "Access to std::array data as built-in array type",
www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#930
Which says:
> [ 2009-10 Santa Cruz: ]
> Mark as NAD. There was not enough consensus that this was
> sufficiently useful. There are known other ways to do this,
> such as small inline conversion functions.
So I wonder if user_namespace::get_c_array does the job, as such a
conversion function. Especially I would like to know if the
static_cast<T(*)[N]> therein is okay.
Kind regards,
Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware <http://www.xs4all.nl/%7End/dekkerware>
Scientific programmer at LKEB, Leiden University Medical Center
--
[ 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<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Anders Dalvander <google@dalvander.com>
Date: Wed, 2 Dec 2009 22:24:43 CST Raw View
On Dec 1, 11:38 pm, "Niels Dekker - no reply address"
<inva...@this.is.invalid> wrote:
> Can someone please tell me if the following function,
> user_namespace::get_c_array, is guaranteed to get a valid reference to a
> built-in array from a non-empty C++0x std::array?
It may not work if N == 0.
Regards,
Anders Dalvander
--
[ 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 ]
Author: "Niels Dekker - no reply address" <invalid@this.is.invalid>
Date: Fri, 4 Dec 2009 02:35:01 CST Raw View
>> Can someone please tell me if the following function,
>> user_namespace::get_c_array, is guaranteed to get a
>> valid reference to a built-in array from a non-empty
>> C++0x std::array?
Anders Dalvander replied:
> It may not work if N == 0.
Thanks, but does it work for a non-empty std::array? Is it safe to convert
the pointer returned by std::array<T,N>::data() to a reference of type
T(&)[N], when N > 0? Aren't there any possible alignment issues that might
make such a reference useless?
Kind regards,
Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center
--
[ 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 ]
Author: Anders Dalvander <google@dalvander.com>
Date: Fri, 4 Dec 2009 13:13:48 CST Raw View
On Dec 4, 9:35 am, "Niels Dekker - no reply address"
<inva...@this.is.invalid> wrote:
> Thanks, but does it work for a non-empty std::array? Is it safe to convert
> the pointer returned by std::array<T,N>::data() to a reference of type
> T(&)[N], when N > 0? Aren't there any possible alignment issues that might
> make such a reference useless?
I'm unsure of this, but in the latest c++ working draft (N3000)
23.3.1 defines std::array with the public member T elems[N]. Is your
function actually needed if this is true?
In the WD the std::array::elems member has the comment "exposition
only" though, English isn't my native language so unfortunately I
don't fully understand that comment and what it means.
Regards,
Anders Dalvander
--
[ 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 ]
Author: "Bo Persson" <bop@gmb.dk>
Date: Sat, 5 Dec 2009 22:32:31 CST Raw View
Anders Dalvander wrote:
> On Dec 4, 9:35 am, "Niels Dekker - no reply address"
> <inva...@this.is.invalid> wrote:
>> Thanks, but does it work for a non-empty std::array? Is it safe to
>> convert the pointer returned by std::array<T,N>::data() to a
>> reference of type T(&)[N], when N > 0? Aren't there any possible
>> alignment issues that might make such a reference useless?
>
> I'm unsure of this, but in the latest c++ working draft (N3000)
> 23.3.1 defines std::array with the public member T elems[N]. Is
> your function actually needed if this is true?
>
> In the WD the std::array::elems member has the comment "exposition
> only" though, English isn't my native language so unfortunately I
> don't fully understand that comment and what it means.
>
It means that it is not a requirement to actually have a member of
that name. It is not part of the public interface (even if it is a
public member).
Bo Persson
--
[ 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 ]
Author: "Niels Dekker - no reply address" <invalid@this.is.invalid>
Date: Sat, 5 Dec 2009 22:32:10 CST Raw View
>> Is it safe to convert the pointer returned by
>> std::array<T,N>::data() to a reference of type T(&)[N],
>> when N > 0?
Anders Dalvander wrote:
> I'm unsure of this, but in the latest c++ working draft (N3000)
> 23.3.1 defines std::array with the public member T elems[N]. Is
> your function actually needed if this is true?
>
> In the WD the std::array::elems member has the comment
> "exposition only" though [...]
That's exactly the problem! N3000, 23.3.1, Class template array [array] has
a note, that clarifies the meaning of "exposition only":
[ Note: The member variable elems is shown for exposition only, to
emphasize that array is a class aggregate. The name elems is not part of
array's interface. -end note ]
And indeed, I've already seen two different std::tr1::array implementations
that do not have a data member named "elems". That's why I submitted
www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#930 but unfortunately
it didn't make it. So now I would like to know how to write a conversion
function, to get a reference to the built-in array.
Kind regards,
Niels
--
[ 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 ]