Topic: Do templates really require named types?


Author: Tom.Horsley@worldnet.att.net (Thomas A. Horsley)
Date: 1999/05/02
Raw View
I decided a template member function could simplify my X11 programming where
I always have to fill in arbitrary sized arrays of "Arg" structs to create
widgets. So I wrote a little class with a dynamic array of "Arg" structs and
a template member function to append to the end, something like this:

class whatever {
...
   template <typename valtype>
   void
   Append(String valname, valtype valval) {
         int x = length + 1;
         XtSetArg(array[x], valname, valval);
      }
...
};

As soon as I tried to use it to do something like:

   whatever arg;

   arg.Append(XmNtopAttachment, XmATTACH_FORM);

I got an error from the compiler telling me I cannot use templates with
types that don't have names. Apparently the X11 headers define all the silly
constants like XmATTACH_FORM by using anonymous enum types, and that's what
the compiler hates.

Does the standard really say this?

We've got templates that can slice and dice and magically spring into
existence without any explicit instantiation, but they can't deal with
anonymous types?

The very first real world use I have for a template and I find they don't
work? Sheesh!

Is it some kind of consession to compiler implementors? (Perhaps nobody
could figure out how to name the template so you could link the program if
it happened to be out of line rather than inline?). Of course, if that's the
reason, then you'd have to ask why "export" was included in the
standard... :-).

Or maybe its just a compiler bug? (I hope).
--
>>==>> The *Best* political site <URL:http://www.vote-smart.org/> >>==+
      email: Tom.Horsley@worldnet.att.net icbm: Delray Beach, FL      |
<URL:http://home.att.net/~Tom.Horsley> Free Software and Politics <<==+
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/05/03
Raw View
Thomas A. Horsley wrote:
TH>
TH> I decided a template member function could simplify my X11 programming wherTHe
TH> I always have to fill in arbitrary sized arrays of "Arg" structs to create
TH> widgets. So I wrote a little class with a dynamic array of "Arg" structs and
TH> a template member function to append to the end, something like this:
TH>
TH> class whatever {
TH> ...
TH>    template <typename valtype>
TH>    void
TH>    Append(String valname, valtype valval) {
TH>          int x = length + 1;
TH>          XtSetArg(array[x], valname, valval);
TH>       }
TH> ...
TH> };
TH>
TH> As soon as I tried to use it to do something like:
TH>
TH>    whatever arg;
TH>
TH>    arg.Append(XmNtopAttachment, XmATTACH_FORM);
TH>
TH> I got an error from the compiler telling me I cannot use templates with
TH> types that don't have names. Apparently the X11 headers define all the silly
TH> constants like XmATTACH_FORM by using anonymous enum types, and that's what
TH> the compiler hates.
TH>
TH> Does the standard really say this?

Annoying! Section 14.3.1 p1 says "A local type, a type with no linkage,
an unnamed type or a type compounded from any of these types shall not
be used as a template-argument for a template type-parameter."

Offhand, I can't think of a good, portable workaround. The best I can do
is suggest a static_cast<> of the enumerator to an appropriate integer
type, but there's no portable way to determine that type.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/05/03
Raw View
James Kuyper wrote:
>
> Thomas A. Horsley wrote:
....
> TH> types that don't have names. Apparently the X11 headers define all the silly
> TH> constants like XmATTACH_FORM by using anonymous enum types, and that's what
> TH> the compiler hates.
> TH>
> TH> Does the standard really say this?
>
> Annoying! Section 14.3.1 p1 says "A local type, a type with no linkage,
> an unnamed type or a type compounded from any of these types shall not
> be used as a template-argument for a template type-parameter."
>
> Offhand, I can't think of a good, portable workaround. The best I can do
> is suggest a static_cast<> of the enumerator to an appropriate integer
> type, but there's no portable way to determine that type.

I was so busy thinking about the C++ issues, that I forgot to think
about the broader context. There's a perfectly straightforward way to
determine the appropriate type. Look at the X functions that require
such constants as arguments. Find out what type has been declared for
recieving them. That type is perfectly safe to use for this purpose.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]