Re: typename and sizeof
On Dec 5, 3:23 am, "Mycroft Holmes" <m.hol...@nospam.it> wrote:
Hi to all,
quite surprisingly, we discovered that inside 'sizeof', the keyword
'typename' is not required.
apparently, sizeof does not care if -say- A<T>::type is a type(def) or a
static member.
That's because it could be valid either way.
for example, this compiles in Comeau compiler online:
template <class T>
struct A
{ typedef char type;};
template <class T>
struct B
{
static const int N = sizeof(A<T>::type); // note 'typename' missing here
Note that this is clearly not an error at this point. After all, as
you note, an explicit specialization might make this unambiguously
valid later on.
};
int main()
{
return B<int>::N;
}
while it obviously gives error if we try to declare a member in B, whose
type is A<T>::type (without 'typename').
it still compiles if I add a partial specialization where 'type' is not a
type
template <>
struct A<int>
{ static const int type = -99; };
Right, it's clearly valid in that case since "A<int>::type" isn't a
type name in that case.
Is this fact documented in the standard?
Not in so many words, but one can read the standard as making your
example valid. Specifically, it specifies that if a "typename
<dependent-type-name>" construct substitutes to a non-type during
instantiation, then the program is ill-formed. However, it doesn't
specify the converse (your case): If a <dependent-name> is interpreted
as a nontype in the generic parse, its not clear that it cannot be
interpreted as a type at instantiation time.
I'm pretty sure that the _intent_ is that this be invalid, but unless
I'm overlooking something, the TC1 words don't realize that intent.
(Part of the elephantic problem is that the standard doesn't actually
say what template instantiation is. %^/)
Daveed
I think it's not explicitly mentioned in "C++ Templates - The Complete
Guide" byVandevoorde/Josuttis.