Move semantics problem.

C

cola

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::eek:perator = (
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::eek:perator = ( 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::eek:perator = ( proxy_mover_t pm )
{
m_internals = pm.m_internals;
return *this;
}

mover_t::eek:perator 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)'
 
H

Howard Hinnant

I need to implement move semantics followinghttp://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]

If you're willing to put -std=c++0x on the command line, then the
following will do what you want (Disclaimer: I didn't actually test
this on gcc 4.5):

#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(std::move(internals.m_resource))
{
}

internals_t &
internals_t::eek:perator = (
internals_t && internals )
{
m_resource = std::move( internals.m_resource );
return *this;
}

internals_t::internals_t(
const std::string & res )
:
m_resource( res )
{
}

class mover_t
{
public:
mover_t(
const std::string & res )
:
m_internals( res )
{}
mover_t(
mover_t && m );
mover_t &
operator = ( mover_t && m );
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( std::move(m.m_internals) )
{
}

mover_t &
mover_t::eek:perator = ( mover_t && m )
{
m_internals = std::move(m.m_internals);
return *this;
}

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();
}
 
C

cola

Howard, thanx for fresh idea, but it didn't help :(

The real source that causes the trouble is just a piece of a poject,
and it would be great not to change compiler options.

I need to implement move semantics followinghttp://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]

If you're willing to put -std=c++0x on the command line, then the
following will do what you want (Disclaimer: I didn't actually test
this on gcc 4.5):

#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(std::move(internals.m_resource))
{

}

internals_t &
internals_t::eek:perator = (
        internals_t && internals )
{
        m_resource = std::move( internals.m_resource );
        return *this;

}

internals_t::internals_t(
        const std::string & res )
        :
                m_resource( res )
{

}

class mover_t
{
        public:
                mover_t(
                        const std::string & res )
                        :
                                m_internals( res )
                {}
                mover_t(
                        mover_t && m );
                mover_t &
                operator = ( mover_t && m );
                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( std::move(m.m_internals) )
{

}

mover_t &
mover_t::eek:perator = ( mover_t && m )
{
        m_internals = std::move(m.m_internals);
        return *this;

}

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();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top