Re: typename and sizeof

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



vandevoorde@xxxxxxxxx wrote:
On Dec 5, 3:23 am, "Mycroft Holmes" <m.hol...@xxxxxxxxx> 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. %^/)

Interesting. By the same argument the following code is legal:

int i;

template <class T> void f() {
T::x * i; //declaration or multiplication!?
}

struct Foo {
typedef int x;
};

struct Bar {
static int const x = 5;
};

int const Bar::x;

int main() {
f<Bar>(); //multiplication
f<Foo>(); //declaration!
}

That's certainly surprising that the same code can have two completely different meanings, despite all the jiggery pokery of dependent/non-dependent names! I note that Comeau online is happy with it and it sounds like the standard doesn't (currently) forbid it.

Tom
.



Relevant Pages

  • Re: Template function type for arithmetic operators for elementary types
    ... Not all compilers will support this yet because of the template-template ... template ... struct PlusEqual ... > int main ...
    (comp.lang.cpp)
  • Re: Function template + overloading + polymorphism
    ... > struct B; ... The template is a better match because: ... int F; ... typename Metrowerks::restrict_to ...
    (comp.lang.cpp)
  • Re: Need some clever C++ idea
    ... int type; ... can't rewrite the entire table initialization section (change must ... can't add a lot of additional data members to the struct X (tables ... though you might be able to fix this doing some more template ...
    (microsoft.public.vc.language)
  • Re: function template
    ... > I was working with a simple function template to find the min of two values. ... struct Promote ... template struct Promote{typedef unsigned long ... template struct Promote{typedef int ...
    (comp.lang.cpp)
  • anonymous template definition
    ... templatestruct TypeSelectorImpl ... templatestruct Selected ... template<> struct TypeSelectorImpl ... typedef char SmallArray; ...
    (comp.lang.cpp)