This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [Warning] When passing const reference and default value
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Mathieu Malaterre <mmalater at nycap dot rr dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 1 Nov 2004 15:35:53 -0800
- Subject: Re: [Warning] When passing const reference and default value
- References: <4186B41E.2060300@nycap.rr.com>
On Mon, Nov 01, 2004 at 05:09:34PM -0500, Mathieu Malaterre wrote:
> Hello,
>
> I was just wondering should the following code (*) produce a warning
> when being compiled or not? I would tend to say yes, it should. If not
> could someone explain me how the compiler could produce a string
> reference out of the default const char * value ?
This is a basic C++ question. The code is valid and should not produce
a warning. The initializer for a const reference might require a
conversion; if it does, a temporary is created and the const reference
points to the temporary. It is quite common to bind C charater strings
to const std::string references.
> void foo(std::string const & s = "bar" )
> {
> std::cout << s << std::endl;
> }