Prev: ANNOUNCE: just::thread library V1.3 released
Next: "Scoping" variables the middle of their "natural" scope - RAII - is it a good practice?
From: Andrew on 15 Jan 2010 10:32 I am getting this warning from g++ 4.4.2 and yes indeed it is taking the address of a temporary, but the temporary ought to still be alive when it matters AFAIC. So can someone tell why what, if anything, I am doing wrong. Here is my example: #include <iostream> #include <string> class Temporary { public: Temporary(const std::string& str) : m_str(str) {} std::string str() const { return m_str;} private: std::string m_str; }; class Example { public: void doSomething(Temporary* temp) { std::cout << "temp str = " << temp->str() << std::endl; } }; int main(int argc, char* argv[]) { Example example; example.doSomething(&Temporary("Hello World.")); return 0; } Regards, Andrew Marlow -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alf P. Steinbach on 15 Jan 2010 11:11 * Andrew: > I am getting this warning from g++ 4.4.2 and yes indeed it is taking > the address of a temporary, but the temporary ought to still be alive > when it matters AFAIC. So can someone tell why what, if anything, I am > doing wrong. Here is my example: > > #include <iostream> > #include <string> > > class Temporary { > public: > Temporary(const std::string& str) : m_str(str) {} > std::string str() const { return m_str;} > private: > std::string m_str; > }; > > class Example { > public: > void doSomething(Temporary* temp) > { > std::cout << "temp str = " << temp->str() << std::endl; > } > }; > > int main(int argc, char* argv[]) > { > Example example; > example.doSomething(&Temporary("Hello World.")); > > return 0; > } In C++98 the built-in address operator does not apply to rvalues, hence the warning (presumably it says something about language extension or such?). You could do something like template< typename T > T& tempRef( T const& t ) { return const_cast<T&>( t ); } But better just replace 'Temporary*' with 'Temporary const&'. Cheers & hth., - Alf -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Öö Tiib on 17 Jan 2010 21:35 On Jan 16, 5:32 am, Andrew <marlow.and...(a)googlemail.com> wrote: > I am getting this warning from g++ 4.4.2 and yes indeed it is taking > the address of a temporary, but the temporary ought to still be alive > when it matters AFAIC. So can someone tell why what, if anything, I am > doing wrong. Here is my example: > > #include <iostream> > #include <string> > > class Temporary { > public: > Temporary(const std::string& str) : m_str(str) {} > std::string str() const { return m_str;} > private: > std::string m_str; > > }; > > class Example { > public: > void doSomething(Temporary* temp) > { > std::cout << "temp str = " << temp->str() << std::endl; > } > > }; > > int main(int argc, char* argv[]) > { > Example example; > example.doSomething(&Temporary("Hello World.")); > > return 0; > > } > > Regards, > > Andrew Marlow You cannot use temporarily constructed object as lvalue by standard. Taking non-const pointer to one implies that you may access your temporary through that pointer as lvalue. Take const reference and you will get no warnings. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Andrew on 17 Jan 2010 21:45
On 16 Jan, 04:11, "Alf P. Steinbach" <al...(a)start.no> wrote: > > I am getting this warning from g++ 4.4.2 and yes indeed it is taking > > the address of a temporary, but the temporary ought to still be alive > > when it matters AFAIC. So can someone tell why what, if anything, I am > > doing wrong. > In C++98 the built-in address operator does not apply to rvalues, hence the > warning (presumably it says something about language extension or such?). Ah, yes, this is what I wanted to know. Thanks. > You could do something like > > template< typename T > > T& tempRef( T const& t ) { return const_cast<T&>( t ); } > > But better just replace 'Temporary*' with 'Temporary const&'. Unfortunately, not. The function that takes a pointer is from outside our project and cannot be changed. I was hoping for something where the function signature would remain unaltered. Perhaps the only way is to introduce a named automatic variable, which is what the original code was trying to avoid. -Andrew -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |