Topic: != template and enumerated types


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/09/29
Raw View
J. Kanze wrote:
>
> "Michael Taylor" <mbtaylor@microsoft.com> writes:
>
>  |>  Steve Clamage <stephen.clamage_nospam@eng.sun.com> wrote in article
>  |>  <3429eef4.25635521@engnews1.eng>...
>  |>
>  |>  > But a template cannot be instantiated on a local type, ...
>  |>
>  |>  I found this a surprising restriction. Is this a restriction on both
>  |>  template class and template functions?
>
> Yes.
>
>  |>  Why isn't it allowed? I imagine it'd be easier not to allow it, since
>  |>  otherwise there could be name collisions if two different scopes define the
>  |>  same named type, but that problem seems solvable.
>
> Not simply.  It's more than a problem of naming -- local types don't
> have linkage, and aren't visible outside of the function.  But the
> template will be instantiated outside of the function.  So how do you
> instantiate a template over a type that it cannot see.

I don't think this is so much a problem. You could simply add a
visibility rule that allows templates to see their template types
even if they wouldn't see them otherwise. As the type is local,
there is no way to get the type info to any other part except the
template itself (and the block where the template is defined).
BTW, it's new to me that types have linkage at all. Are you shure
about this?
At least the linker doesn't know about typenames, just about mangled
function names. For template instantiations on local types, there
could be internal linkage and a simple extension for name mangling,
f.ex. by just counting the instantiations in the file, and using this
as template parameter name.

Could you please give some code using templates on local types that
would be problematic?
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/09/30
Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:

|>  BTW, it's new to me that types have linkage at all. Are you shure
|>  about this?

Yep.  This is a difference from C: C++ always uses name equivalence of
types (which requires linkage), whereas C has the notion of "compatible
types".  (See 3.5/4 of the CD.)

As an example of where this might (formally) make a difference, suppose
you define a struct X at file scope in two different modules, and define
it differently.  In C, this is legal; in C++, it results in undefined
behavior.  (In practice, this isn't the compatibility problem it would
seem to be.  In C, if the definitions are really different, and not
compatible, there are significant limits as to what you can do with the
struct without incuring undefined behavior.  And if you stay within
those limits, it is almost certain that the "undefined" behavior in C++
will be to do exactly what C would have done.)

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
        I'm looking for a job -- Je recherche du travail
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/09/25
Raw View
"Michael Taylor" <mbtaylor@microsoft.com> writes:

 |>  Steve Clamage <stephen.clamage_nospam@eng.sun.com> wrote in article
 |>  <3429eef4.25635521@engnews1.eng>...
 |>
 |>  > But a template cannot be instantiated on a local type, ...
 |>
 |>  I found this a surprising restriction. Is this a restriction on both
 |>  template class and template functions?

Yes.

 |>  Why isn't it allowed? I imagine it'd be easier not to allow it, since
 |>  otherwise there could be name collisions if two different scopes define the
 |>  same named type, but that problem seems solvable.

Not simply.  It's more than a problem of naming -- local types don't
have linkage, and aren't visible outside of the function.  But the
template will be instantiated outside of the function.  So how do you
instantiate a template over a type that it cannot see.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
        I'm looking for a job -- Je recherche du travail
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.Sun.COM (Steve Clamage)
Date: 1997/09/25
Raw View
On 23 Sep 97 20:09:12 GMT, "Michael Taylor" <mbtaylor@microsoft.com>
wrote:

>Steve Clamage <stephen.clamage_nospam@eng.sun.com> wrote in article
><3429eef4.25635521@engnews1.eng>...
>
>> But a template cannot be instantiated on a local type, ...
>
>I found this a surprising restriction. Is this a restriction on both
>template class and template functions?

In the current (non-public) version of the draft, section  14.3.1
"Template type arguments" 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."  A
similar sentence is in CD2 section 14.3 paragraph 9. It applies to all
templates.

One problem is synthesizing a unique name. (Suppose you have an
unnamed local type in a static function in a source file, and the
source file name is not unique in the program.) Another problem is
what it ought to mean, for example, if a template with external
linkage is instantiated on such a type.

Instead of forcing implementations to cope with these issues, it
seemed simpler (and not overly-restrictive) to limit the kinds of
types on which a template can be instantiated.
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: abell@nospam.mindspring.com (Andrew Bell)
Date: 1997/09/20
Raw View
I'm trying to compile the following code under MS Visual C++ 5.0, and
I'm have difficulty deciphering what the DWP says about this.
---
template <class Type> bool operator!=(const Type &x, const Type &y)
{ return !(x == y); }

int main()
{
 enum MyEnum { eFalse, eTrue };
 MyEnum m1 = eFalse, m2 = eTrue;
 if ( m1 != m2 )
  return 1;
 return 0;
}
---

Compiling this code gets the error
STLTest.cpp
C:\Dev\TestProjects\STLTest\STLTest\STLTest.cpp(8) : error C2918:
'MyEnum' : illegal use of local type in template instantiation
Error executing cl.exe.

If I remove the != template at the top, it compiles successfully.   Is
this how C++ is supposed to behave under the new (soon to be)
standard?

Andrew Bell
abell@nospam.mindspring.com
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/09/23
Raw View
On 20 Sep 97 00:50:00 GMT, abell@nospam.mindspring.com (Andrew Bell)
wrote:

>I'm trying to compile the following code under MS Visual C++ 5.0, and
>I'm have difficulty deciphering what the DWP says about this.
>---
>template <class Type> bool operator!=(const Type &x, const Type &y)
>{ return !(x == y); }
>
>int main()
>{
> enum MyEnum { eFalse, eTrue };
> MyEnum m1 = eFalse, m2 = eTrue;
> if ( m1 != m2 )
>  return 1;
> return 0;
>}
>---
>
>Compiling this code gets the error
>STLTest.cpp
>C:\Dev\TestProjects\STLTest\STLTest\STLTest.cpp(8) : error C2918:
>'MyEnum' : illegal use of local type in template instantiation
>Error executing cl.exe.

I believe the compiler is correct.

The built-in comparison operators first promote an enum to an
arithmetic type. An overloaded comparison operator taking the enum
type provides an exact match, and so is preferred. The consequences
are that you can write your own comparison operator for enums and be
sure it will be used in preference to the built-in comparison.

The template provides an exact match with no promotions, and so is
preferred.  But a template cannot be instantiated on a local type, so
the code is in error.

You can remove the template declaration, or move the enum declaration
out of local scope.
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/09/23
Raw View
[Nathan, isn't that an interresting argument for defining
comparison operators on enums ?]

Andrew Bell <abell@nospam.mindspring.com> writes:

> I'm trying to compile the following code under MS Visual C++ 5.0, and
> I'm have difficulty deciphering what the DWP says about this.
> ---
> template <class Type> bool operator!=(const Type &x, const Type &y)
> { return !(x == y); }
>
> int main()
> {
>  enum MyEnum { eFalse, eTrue };
>  MyEnum m1 = eFalse, m2 = eTrue;
>  if ( m1 != m2 )
>   return 1;
>  return 0;
> }
> ---
>
> Compiling this code gets the error
> STLTest.cpp
> C:\Dev\TestProjects\STLTest\STLTest\STLTest.cpp(8) : error C2918:
> 'MyEnum' : illegal use of local type in template instantiation
> Error executing cl.exe.
>
> If I remove the != template at the top, it compiles successfully.   Is
> this how C++ is supposed to behave under the new (soon to be)
> standard?

This is still under discution, so I can't give a complete reply.

Function:                    Rank:                      Exists:

operator!= (int, int)        enum to int conversion     yes
template operator!=          exact match, template      yes
operator!= (MyEnum, MyEnum)  exact match, non-template  ?

If it exists, the third match is prefered, and there is no
problem. If it doesn't, then the second is choosed, and
a template is instanciated on a local type; but a local type
doesn't have a qualified name, so IMO this is illegal
(I think the case where the template source is accessible
could be made legal, but I don't ask for that.)

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Michael Taylor" <mbtaylor@microsoft.com>
Date: 1997/09/23
Raw View
Steve Clamage <stephen.clamage_nospam@eng.sun.com> wrote in article
<3429eef4.25635521@engnews1.eng>...

> But a template cannot be instantiated on a local type, ...

I found this a surprising restriction. Is this a restriction on both
template class and template functions?

Why isn't it allowed? I imagine it'd be easier not to allow it, since
otherwise there could be name collisions if two different scopes define the
same named type, but that problem seems solvable.

I've looked through Stroustrup 3e and the ARM for more information but
didn't find anything. In fact, Stroustrup 3e doesn't appear to have much
about local types at all.

I don't have my copy of D&E handy. Does it have anything about this?
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]