Re: add inner const to arrays T[]...[]
On 2011-12-12 23:47, Lorenzo wrote:
On Dec 12, 4:12 am, Daniel Kr?gler<daniel.krueg...@googlemail.com>
wrote:
On 2011-12-12 02:47, Lorenzo wrote:
I would like to write a metafunction to add const to array types T
[]...[]. Something like this:
template< typename T>
struct add_inner_const {
typedef typename add_const<T>::type type;
};
What is template add_const supposed to be in this example? Is this
std::add_const? If yes, why are you not using it instead of reinventing
the wheel?
Yes, add_const is std::add_const. Wouldn't std::add_const<T[]>::type
make "T[] const" instead of "T const[]"? In other words, I think
std::add_const adds const at the outer and not at the inner type
level... Is my understanding correct?
Arrays are funny types in many regards, and this is one further
property: There is no difference between a const array of type E and a
(non-const) array type of const E. While this is long known, the core
language is still not clear in all respects, therefore
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1059
exists. But the outcome won't have impact on your example.
If for some reasons you only want to capture array types you can use a
partial specialization:
template<typename T>
struct add_inner_const;
template<typename T, std::size_t N>
struct add_inner_const<T[N]> {
typedef T const type[N];
};
Yes, I realized this yesterday after sending this email :) The next
question is if I can avoid writing specializations to handle
multidimensional arrays:
template<typename T, unsigned I0> struct add_inner_const< T[I0]>
{...};
template<typename T, unsigned I0, unsigned I1> struct add_inner_const<
T[I0][I1]> {...};
template<typename T, unsigned I0, unsigned I1, unsigned I2> struct
add_inner_const< T[I0][I1][I2]> {...};
...
Is there a better way do this also so to handle /any/ number of
dimensions?
You have not read my following sentence, right? ;-)
This will properly handle array of any (known) dimension and rank.
This should answer your previous question, I think.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]