Topic: const and non-const arguments to constructors


Author: "Martijn Lievaart" <mlievaar@orion.nl>
Date: 1998/02/12
Raw View
I have two questions about const arguments to ctors (and functions in
general?)

/****************************************************/
First an academic question.

The following code doesn't compile with any compiler I use (gcc, Sun, msvc).
The problem is that the 'const int' is considerd the same as 'int'.
Can anyone explain why?

class A1
{
public:
     A1(int p);
     A1(const int p);
};
A1::A1(int p)
{
}
A1::A1(const int p)
{
}
A1 a1(1);


/****************************************************/

This gets a real problem here, msvc doesn't compile this, but the others do.
I would say this is legal. I have to port this code to msvc so any comments
would be greatly appriciated.

// Helper
template <class T> class dumdum
{
};

// problem here
template <class T> class B3
{
public:
     B3(dumdum<T> * p);
     B3(const dumdum<T> * p);
};

template <class T> B3<T>::B3(dumdum<T> * p)
{
}

template <class T> B3<T>::B3(const dumdum<T> * p)
{
}

dumdum<int> dummy;
B3<int> b3(&dummy);

/****************************************************/
Please reply by post and by email.
Thx in advance,
Martijn Lievaart




[ 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: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/02/15
Raw View
Martijn Lievaart writes:

> The problem is that the 'const int' is considerd the same as 'int'.
> Can anyone explain why?

The C++ Standard says that any CV-qualifier that modifies the argument
type is discarded, it does not affect the type of the function, just
the type of the argument inside function scope.  This means the type
of `void foo(const int p, char const *const q)' is
`void(int, char const *)'.

Note that only CV-qualifiers that modifies the argument type is
discarded; those that modify a pointed-to or referred-to type are
not.

> This gets a real problem here, msvc doesn't compile this, but the others do.
> I would say this is legal.

> template <class T> class B3
> {
> public:
>      B3(dumdum<T> * p);
>      B3(const dumdum<T> * p);
> };

This is legal.  MSVC is broken.  Try to typedef const dumdum<T> to
some other name inside the template body; this might help.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ 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: "Martijn Lievaart" <mlievaar@orion.nl>
Date: 1998/02/16
Raw View
In follow up to myself I can happily say, both solved!

problem 1 was nicely explained by several people.
problem 2 went away with SP3 for DevStudio. So it was a bug in msvc.

Thx everyone for the help.
Martijn Lievaart, mlievaar@nospam.orion.nl



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