I am not able to understand why the object creation n my below code is wrongly implemented:
#include <iostream>
#include <string>
class A
{
public:
A(std::string apptype) : m_apptype(apptype)
{
std::cout << m_apptype << std::endl;
}
A(std::string&& apptype) : m_apptype(apptype)
{
std::cout << m_apptype << std::endl;
}
private:
std::string m_apptype;
};
int main()
{
A(std::string("Test"));
return 0;
}
I get the below error when I compile my code:
$ c++ Calender.cpp
Calender.cpp:10:14: error: expected ',' or '...' before '&&' token
A(std::string&& apptype) : m_apptype(apptype)
^
Calender.cpp:10:1: error: 'A::A(std::string)' cannot be overloaded
A(std::string&& apptype) : m_apptype(apptype)
^
Calender.cpp:6:1: error: with 'A::A(std::string)'
A(std::string apptype) : m_apptype(apptype)
^
Calender.cpp: In constructor 'A::A(std::string)':
Calender.cpp:10:38: error: 'apptype' was not declared in this scope
A(std::string&& apptype) : m_apptype(apptype)
^
std::mapin there. And it looks like your compiler is configured for C++03, and chokes on the rvalue reference.A(std::string&& apptype) : m_apptype(std::move(apptype)) {}, you are copying it now, not moving. And you do not actually need it, you can just move argument when you passstd::stringby value. This ctor would make sense when first one acceptsconst std::string &