Re: typename and sizeof
"Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.com> wrote in message
news:eeg$ZryNIHA.1204@TK2MSFTNGP03.phx.gbl...
I doubt it. I certainly don't recall seeing it, and I can't think why
sizeof would somehow not require specification of dependent names that are
types.
well, 'A<T>::type' may be a type or a member, but
IMHO it's a bug in the EDG front end used by Comeau C++ - you could report
it to comeau@comeaucomputing.com.
strangely enough, this "hack" is used: for example, it's needed for static
assertions to work (and I think boost uses it).
roughly speaking, all static assertion implementations are based on an
incomplete type:
template <bool B> struct static_assert { typedef char type; };
template <> struct static_assert<false>;
from this point on, there are 2 alternative strategies:
1) the mosty common is
template <size_t N> struct dummy {};
#define STATIC_ASSERT(COND) typedef dummy<
sizeof(static_assert<(COND)>::type) > some_funny_name_t_
this method has some advantages: STATIC_ASSERT can be placed ad namespace
level, at class level and at block level;
the standard grands that you can redefine the same typedef multiple times,
so you can also have multiple STATIC_ASSERTs in the same place.
however there's a problem with templates: if I write STATIC_ASSERT(true),
then static_assert<(COND)>::type is not a dependent name, so you must NOT
prepend 'typename', but if I'm inside -say- vector<T> and I write
static_assert<(...something that depends on T...)>::type, then I _should_
write typename
2) the second (less common)
#define STATIC_ASSERT(COND) sizeof(static_assert<(COND)>)
this has no restriction on COND being dependent or not, but it can be used
only at block level; at higher level, you have to write a cumbersome
assignment:
static const size_t ASSERT1 = STATIC_ASSERT(sizeof(char)==1);
btw, this has some minor syntax advantage (which personally I don't like):
for example you can chain assertions:
static const size_t ASSERT1 = STATIC_ASSERT(sizeof(char)==1) &&
STATIC_ASSERT(sizeof(char)<2);
however I suspect a reasonable compromise would be: use method 1, but
replace 'type' with a static const int named -say- 'value'.
anyway, I'll post a message to comeau (by enterprise policy, we have
restricted access to newsgroups, so I can't ask in comp.lang.*)
thanks to everyone for the hints
MH