Topic: My compiler vendor (Metrowerks) thinks this is not a bug.


Author: darin@goonsquad.spies.com (Darin Adler)
Date: 1996/12/26
Raw View
Are "const int" and "int" different enough to make the following code
snippet illegal?

    template <class T>
    class simple
    {
      public:
        int only_one_member;
    };

    void test()
    {
        simple<int> without_const;
        simple<const int> with_const;

        without_const = with_const; // Legal or error?
    }

My compiler vendor (Metrowerks) says that the assignment is illegal, but
something tells me it could be a problem with the compiler, not with the
code.  Is there something in the C++ standard that sheds light on this?
I've tried to read the relevant parts of the standard, but I can't figure
it out.

The evidence that the support folks at Metrowerks provided to show that
this is not a bug was detail of the mangled names.  This seems like
evidence about the details of implementation, but sheds little light on
what the standard requires.

PS:  This question grew out of a practical problem that came up when using
the standard template library -- trying to put a pair<X, Y> object into a
pair<const X, Y> parameter.
---
[ 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: ncm@best.com (Nathan Myers)
Date: 1996/12/27
Raw View
darin@goonsquad.spies.com (Darin Adler) writes:

>Are "const int" and "int" different enough to make the following code
>snippet illegal?
>
>    template <class T> class simple {   };
>    void test() { simple<int> y; simple<const int> n; n = y; }

Yes, this is bad code.  y and n have different types, so there is no
assignment operator pre-defined for them.

>PS:  This question grew out of a practical problem that came up when using
>the standard template library -- trying to put a pair<X, Y> object into a
>pair<const X, Y> parameter.

For pair<>, if you had an up-to-date compiler and a conforming library,
there would be a conversion from the one pair type to the other -- but
that depends on member templates, which Metrowerks (and all other Mac
and PC compilers except Motorola's unreleased version) don't yet support.

If anyone reading this list is in a position to help persuade Motorola
not to discard their PowerPC C++ compiler, please get in touch with them.

Nathan Myers
ncm@cantrip.org


[ 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: jlilley@empathy.com (John Lilley)
Date: 1996/12/27
Raw View
Darin Adler wrote:
>
> Are "const int" and "int" different enough to make the following code
> snippet illegal?
>
>     template <class T> class simple {...};
>     void test() {
>         simple<int> without_const;
>         simple<const int> with_const;
>         without_const = with_const; // Legal or error?
>     }

Here's what I've found in the May96 DWP:

14.1/5: ... The top-level cv-qualifiers on the template-parameter are
ignored when determining it's type.  [ This only applies to non-type
parameters and does not support your case ]

14.4/1: Two template-ids refer to the same class or function if their
template names are identical, they refer to the same template, their
type template-arguments are the same type, and their non-type template
arguments have identical values.


The latter statement shows that with_const and without_const are clearly
different, because "const int" is a different type than "int" regardless
of the fact that they are assignment-compatible or otherwise "similar".

john lilley


[ 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: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1996/12/27
Raw View
darin@goonsquad.spies.com (Darin Adler) writes:

|>  Are "const int" and "int" different enough to make the following code
|>  snippet illegal?

Yes.

|>      template <class T>
|>      class simple
|>      {
|>        public:
|>          int only_one_member;
|>      };
|>
|>      void test()
|>      {
|>          simple<int> without_const;
|>          simple<const int> with_const;
|>
|>          without_const = with_const; // Legal or error?
|>      }
|>
|>  My compiler vendor (Metrowerks) says that the assignment is illegal, but
|>  something tells me it could be a problem with the compiler, not with the
|>  code.  Is there something in the C++ standard that sheds light on this?
|>  I've tried to read the relevant parts of the standard, but I can't figure
|>  it out.

I don't think that there is anything to figure out.  Const int and int
are definitly different types.  This means that there are two distinct
instantiations of the template.  There is an implicit conversion of
const int to int, and in fact, the lvalue to rvalue converions removes
const qualifiers automatically.  On the other hand, the resulting
template instantiations are two distinct and unrelated classes; the only
conversions available are those that you provide yourself.

|>  The evidence that the support folks at Metrowerks provided to show that
|>  this is not a bug was detail of the mangled names.  This seems like
|>  evidence about the details of implementation, but sheds little light on
|>  what the standard requires.

Agreed here.  The support folks at Metroworks seem to have cause and
effect mixed up.  Presumably, the mangled names are different because
the types are different, and not vice versa.

|>  PS:  This question grew out of a practical problem that came up when using
|>  the standard template library -- trying to put a pair<X, Y> object into a
|>  pair<const X, Y> parameter.

The standard uses another new feature to handle this: the "copy"
constructor of pair is a template member.  (But assignment isn't!  Of
course, automatic conversion should take place so that both sides have
the same type.)  Basically, if you can copy the two elements, you can
copy the pair; in this particular case, since you can copy a const X
into an X, you can copy the pair< const X , Y > into pair< X , Y >.

Member templates are a relatively new feature, and may not be supported
by your compiler yet.  Until then, the only solution I can think of is
the somewhat awkward template function:

    template< class T1 , class T2 , class U1 , class U2 >
    inline void
    assignPairs( pair< T1 , T2 >& dst , pair< U1 , U2 > const& src )
    {
     dst = pair< T1 , T2 >( src.first , src.second ) ;
    }

--
James Kanze         home:     kanze@gabi-soft.fr        +33 (0)3 88 14 49 00
                    office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 8 rue des Francs Bourgeois, F-67000 Strasbourg, France
       -- Conseils en informatique industrielle --


[ 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                             ]