Topic: Q: Parameterized typedefs - legal?


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 15 Aug 1994 03:25:48 GMT
Raw View
In article <CuHLM4.DLz@cdf.toronto.edu> g2devi@cdf.toronto.edu (Robert N. Deviasse) writes:
>In article <32gbc8$3ct@trixie.rtpnc.epa.gov>,
>Todd Plessel <plessel@fred.rtpnc.epa.gov> wrote:
>>
>>
>>/*
>>1. Are parameterized typedefs legal C++? (None of my compilers accept them.)
>
>Not the last I heard.

Actually, the last time I checked (which, I admit, was some time ago) a
declaration of the form:

 template <template-formals-list> typedef type-spec identifier;

was allowed by the grammar, and I wasn't able to find anything saying
that such a declaration was ``ill-formed'' (although this may have been
cleaned up by now).

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 17 Aug 1994 02:09:44 GMT
Raw View
rfg@netcom.com (Ronald F. Guilmette) writes:

>g2devi@cdf.toronto.edu (Robert N. Deviasse) writes:
>>Todd Plessel <plessel@fred.rtpnc.epa.gov> wrote:
>>>
>>>1. Are parameterized typedefs legal C++? (None of my compilers accept them.)
>>
>>Not the last I heard.
>
>Actually, the last time I checked (which, I admit, was some time ago) a
>declaration of the form:
>
> template <template-formals-list> typedef type-spec identifier;
>
>was allowed by the grammar, and I wasn't able to find anything saying
>that such a declaration was ``ill-formed'' (although this may have been
>cleaned up by now).

What's the matter Ron, isn't WP 14.2 clear enough for you?

| The declaration in a template-declaration must declare or define a
| function or a class, define a static data member of a template class,
| or define a template member of a class.

Or is it the lack of standardese that you are complaining about?
The restriction was in there in plain english in the ARM, and it's
still there in the committee's working paper.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Thu, 18 Aug 1994 04:12:58 GMT
Raw View
In article <9422912.9383@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>rfg@netcom.com (Ronald F. Guilmette) writes:
>
>>g2devi@cdf.toronto.edu (Robert N. Deviasse) writes:
>>>Todd Plessel <plessel@fred.rtpnc.epa.gov> wrote:
>>>>
>>>>1. Are parameterized typedefs legal C++? (None of my compilers accept them.)
>>>
>>>Not the last I heard.
>>
>>Actually, the last time I checked (which, I admit, was some time ago) a
>>declaration of the form:
>>
>> template <template-formals-list> typedef type-spec identifier;
>>
>>was allowed by the grammar, and I wasn't able to find anything saying
>>that such a declaration was ``ill-formed'' (although this may have been
>>cleaned up by now).
>
>What's the matter Ron, isn't WP 14.2 clear enough for you?
>
>| The declaration in a template-declaration must declare or define a
>| function or a class, define a static data member of a template class,
>| or define a template member of a class.

Yes.  That's clear enough.  (It just wasn't present in the working paper
a few revs ago.  I'm glad to see its in there now.)

I should mention however (while we are on the subject) that the WP *doesn't*
say that a declaration such as:

 template <template-formals-list> typedef type-spec identifier;

is ``ill-formed'' *or* that it such usage must elicit a diagnostic from a
conforming implementation (although it probably should say both these things).

But this is a general problem which is rampant throughout the WP.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -




Author: plessel@fred.rtpnc.epa.gov (Todd Plessel)
Date: 12 Aug 1994 17:26:32 GMT
Raw View

/*
1. Are parameterized typedefs legal C++? (None of my compilers accept them.)
2. Will they be? (In ANSI C++.)
3. Should they be? (I think so - any drawbacks?.)
4. Yes, they are just syntactic sugar and
5. Yes, there are other solutions, but...
   below is an example for illustration:
*/




template <class T> class Node1 // A singly-linked list node.
{
  T t;
  T* next;
  // ...
};

template <class T> class Node2 : public Node1 // A doubly-linked list node.
{
  T* previous;
  // ...
};

template <class T, class N> class AnyList // Parameterized impl. of both.
{
  N* head;
  // ...
  &T operator[]( int );
};

// Here are the typedefs (provided for client convenience):

template <class T> typedef class AnyList<T,Node1<T> > List1<T>; // Single.
template <class T> typedef class AnyList<T,Node2<T> > List2<T>; // Double.

// Note: this would also allow helper classes such as Node1, and Node2 to be
// hidden from the clients.


// Usage by clients:

void withoutParameterizedTypedefs() // Verbose, not encapsulated...
{
  AnyList<int,  Node1<int>  > x1; // Instantiate a singly-linked list of ints.
  AnyList<char, Node2<char> > x2; // Instantiate a doubly-linked list of chars.
  AnyList<int,  Node2<char> > x3; // Woops! Mismatched types...
}

void withNonparameterizedTypedefs() // Still not as convenient...
{
  typedef AnyList<int,  Node1<int>  > intList1;
  typedef AnyList<char, Node2<char> > charList2;
  // ... and so on for each type that will be instantiated...

  intList1  x1; // Instantiate a singly-linked list of ints.
  charList2 x2; // Instantiate a doubly-linked list of chars.
}

void withParameterizedTypedefs() // Better!
{
  List1<int>  x1; // Single.
  List2<char> x2; // Double.
}







Author: g2devi@cdf.toronto.edu (Robert N. Deviasse)
Date: Sat, 13 Aug 1994 18:49:15 GMT
Raw View
In article <32gbc8$3ct@trixie.rtpnc.epa.gov>,
Todd Plessel <plessel@fred.rtpnc.epa.gov> wrote:
>
>
>/*
>1. Are parameterized typedefs legal C++? (None of my compilers accept them.)

Not the last I heard.

>2. Will they be? (In ANSI C++.)

It seems unlikely. There's a large push to standardize existing C++,
and unless a feature is considered to be essential it won't likely
be added.

>3. Should they be? (I think so - any drawbacks?.)

They're convenient but there's a nearly identical idiom that allows
you to do this. (see below)

>4. Yes, they are just syntactic sugar and
>5. Yes, there are other solutions, but...
>   below is an example for illustration:

Actually, there is a better solution.

You can rewrite:
   template<class T>
      typedef Foo<T,Bar<T> > FooBar;

   FooBar<int> x;
as
   template<class T>
     struct FooBar {
        typedef Foo<T,Bar<T> > Type;
     };

   FooBar<int>::Type x;

Hope this helps.

Take care
    Robert
--
/----------------------------------+------------------------------------------\
| Robert N. Deviasse               |"If we have to re-invent the wheel,       |
| EMAIL: g2devi@cdf.utoronto.ca    |  can we at least make it round this time"|
+----------------------------------+------------------------------------------/