Topic: Q: Const Reference as Default Parameter
Author: Dennis Yelle <dennis51@jps.net>
Date: 2000/05/24 Raw View
Ivo 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.
I don't understand your problem.
The program below compiles with no warnings and runs with
gcc version 2.95.2 19991024 (release)
#include <iostream>
class C {
public:
C() {}
};
void f( const C& c=C());
void f( const C& c)
{
}
int main( int argc, char** argv)
{
C c;
f(c);
f();
cout << "Done.\n";
return 0;
}
Dennis Yelle
--
I am a computer programmer and I am looking for a job.
There is a link to my resume here: http://table.jps.net/~vert
---
[ 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: "Ivo" <ivo.penzar@infolink-software.com>
Date: 2000/05/24 Raw View
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.
To apply a fixture, I can declare something like
void f(const C& c=(C&)(const C&)C());
Otherwise, I can overload my function f
void f(const C&);
void f() {C c; f(c);};
In the case I need it frequently, for different classes and their various
constructors (with or without parameters), I can (reluctantly) use a
macro definition:
TEMP_OBJ_INST(T) (T&)(const T&)T
void f(const C& c=TEMP_OBJ_INST(C)());
void g(const D& d=TEMP_OBJ_INST(D)(-1, 3.14));
- where D was another class with public constructor D(int, double).
Is there any other, more elegant way to put it right?
Thanks,
Ivo
---
[ 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 ]