Topic: Const Reference as Default Parameter
Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: 2000/05/24 Raw View
"Ivo" <ivo.penzar@infolink-software.com> wrote...
> Suppose I have a class C and a function
>
> void f(const C&);
>
> Now, I want to provide a default argument for this function,
> something like
>
> void f(const C& c=C());
>
> (provided that class C defines a meaningful public default
constructor).
>
> Unfortunately, this construct is not aligned with the latest C++
standard.
> Indeed, MSVC++ (debug mode only) would report a warning, while GNU C++
> compilers would report an error.
Really? I compiled the following code with both Intel C/C++ Compiler
v4.5
and VC++ v6 sp3, and there were no warnings for the code, and the code
even
seems to work as expected. Could you please quote the Standard
regarding
the construct not being "aligned" with it?
Appreciate it.
--------------------------------------------
#include <iostream>
using namespace std;
class A
{
public:
A() {}
};
int foo(const A& a = A())
{
return reinterpret_cast<int>(&a);
}
int main()
{
A a;
cout << foo(a) << endl;
cout << foo() << endl;
cout << foo() << endl;
cout << foo() << endl;
return 0;
}
-----------------------------------------------
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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: "John Harrison" <jahhaj@dtn.ntl.com>
Date: 2000/05/24 Raw View
Ivo <ivo.penzar@infolink-software.com> wrote in message
news:8gbfo7$3r9$1@as102.tel.hr...
> Suppose I have a class C and a function
>
> void f(const C&);
>
> Now, I want to provide a default argument for this function,
> something like
>
> void f(const C& c=C());
>
> (provided that class C defines a meaningful public default constructor).
>
> Unfortunately, this construct is not aligned with the latest C++ standard.
> Indeed, MSVC++ (debug mode only) would report a warning, while GNU C++
> compilers would report an error.
>
Works on my MSVC++ 6, and looks standard to me. Perhaps you better post the
code your trying to compile.
john
---
[ 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 ]