This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Move semantics problem
- From: Nicolai Grodzitski <utromvecherom at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sun, 17 Oct 2010 15:59:37 +0300
- Subject: Move semantics problem
I need to implement move semantics following
http://www.synesis.com.au/resources/articles/cpp/movectors.pdf
but on gcc 4.5 (MinGW win32) I get compiling error kind of: "error: no
matching function for call to [func_name]"
Where am I wrong?
Single file repro:
----------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
struct internals_t
{
? ? ? ? internals_t();
? ? ? ? internals_t( const std::string & res );
? ? ? ? internals_t(
? ? ? ? ? ? ? ? internals_t & internals );
? ? ? ? internals_t &
? ? ? ? operator = ( internals_t & internals );
? ? ? ? std::string m_resource;
};
internals_t::internals_t()
? ? ? ? :
? ? ? ? ? ? ? ? m_resource( "" )
{
}
internals_t::internals_t(
? ? ? ? internals_t & internals )
{
? ? ? ? m_resource.swap( internals.m_resource );
}
internals_t &
internals_t::operator = (
? ? ? ? internals_t & internals )
{
? ? ? ? m_resource.swap( internals.m_resource );
? ? ? ? return *this;
}
internals_t::internals_t(
? ? ? ? const std::string & res )
? ? ? ? :
? ? ? ? ? ? ? ? m_resource( res )
{
}
class mover_t
{
? ? ? ? public:
? ? ? ? ? ? ? ? struct proxy_mover_t
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? internals_t m_internals;
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? mover_t(
? ? ? ? ? ? ? ? ? ? ? ? const std::string & res )
? ? ? ? ? ? ? ? ? ? ? ? :
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? m_internals( res )
? ? ? ? ? ? ? ? {}
? ? ? ? ? ? ? ? mover_t(
? ? ? ? ? ? ? ? ? ? ? ? mover_t & m );
? ? ? ? ? ? ? ? mover_t &
? ? ? ? ? ? ? ? operator = ( mover_t & m );
? ? ? ? ? ? ? ? mover_t(
? ? ? ? ? ? ? ? ? ? ? ? proxy_mover_t pm );
? ? ? ? ? ? ? ? mover_t &
? ? ? ? ? ? ? ? operator = ( proxy_mover_t pm );
? ? ? ? ? ? ? ? operator proxy_mover_t ();
? ? ? ? ? ? ? ? void
? ? ? ? ? ? ? ? print(
? ? ? ? ? ? ? ? ? ? ? ? const std::string & where )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? std::cout << where << ": "
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? << "this = " << this
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? << "; m_internals = \""
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? << m_internals.m_resource << "\"\n";
? ? ? ? ? ? ? ? }
? ? ? ? private:
? ? ? ? ? ? ? ? internals_t m_internals;
};
mover_t::mover_t(
? ? ? ? mover_t & m )
? ? ? ? :
? ? ? ? ? ? ? ? m_internals( m.m_internals )
{
}
mover_t &
mover_t::operator = ( mover_t & m )
{
? ? ? ? m_internals = m.m_internals;
? ? ? ? return *this;
}
mover_t::mover_t(
? ? ? ? proxy_mover_t pm )
? ? ? ? :
? ? ? ? ? ? ? ? m_internals( pm.m_internals )
{
}
mover_t &
mover_t::operator = ( proxy_mover_t pm )
{
? ? ? ? m_internals = pm.m_internals;
? ? ? ? return *this;
}
mover_t::operator mover_t::proxy_mover_t ()
{
? ? ? ? proxy_mover_t pm;
? ? ? ? pm.m_internals = m_internals;
? ? ? ? return pm;
}
mover_t
make_mover();
mover_t
move_mover();
int
main( int argc, char ** argv )
{
? ? ? ? std::cout << "test_move_semantics\n\n";
? ? ? ? mover_t m = move_mover();
? ? ? ? m.print( "main")
? ? ? ? return 0;
}
mover_t
make_mover()
{
? ? ? ? mover_t m( "make_mover()" );
? ? ? ? m.print( "in make_mover(): " );
? ? ? ? return m;
}
mover_t
move_mover()
{
? ? ? ? return make_mover();
}
----------------------------------------------------------------------------------------------------------------
CMD:
$ g++ -c -o main.o -O2 -DNDEBUG main.cpp
----------------------------------------------------------------------------------------------------------------
ERROR:
Compiling move_semantics/main.cpp ...
main.cpp: In function 'int main(int, char**)':main.cpp:134:25: error:
no matching function for call to
'mover_t::proxy_mover_t::proxy_mover_t(mover_t::proxy_mover_t)'
main.cpp:51:3: note: candidates are: mover_t::proxy_mover_t::proxy_mover_t()
main.cpp:51:3: note:
mover_t::proxy_mover_t::proxy_mover_t(mover_t::proxy_mover_t&)
main.cpp:134:25: error: initializing argument 1 of
'mover_t::mover_t(mover_t::proxy_mover_t)'
main.cpp:137:2: error: expected ';' before 'return'
main.cpp: In function 'mover_t move_mover()':
main.cpp:151:20: error: no matching function for call to
'mover_t::proxy_mover_t::proxy_mover_t(mover_t::proxy_mover_t)'
main.cpp:51:3: note: candidates are: mover_t::proxy_mover_t::proxy_mover_t()
main.cpp:51:3: note:
mover_t::proxy_mover_t::proxy_mover_t(mover_t::proxy_mover_t&)
main.cpp:151:20: error: initializing argument 1 of
'mover_t::mover_t(mover_t::proxy_mover_t)'
--
Thanks, Nicolai