Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9015705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:49:02+00:00 2026-06-16T03:49:02+00:00

With this sample: // test.cpp #include <iostream> #include <vector> #include <utility> using namespace std;

  • 0

With this sample:

// test.cpp

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

class mystring : public string { public:
    mystring() = default;

    mystring(const char* c) : string(c) {}

    mystring(mystring& m) : string(m) { cout << "Reference" << endl; }
    mystring(mystring const & m) : string(m) { cout << "Const reference" << endl; }
    mystring(mystring&& m) : string(move(m)) { cout << "Move" << endl; }
};

int main() {
    mystring a;

    vector<mystring> v{ a };
}

The ouput is:

$ g++ --version | grep "g++"
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
$ g++ -std=c++11 -fno-elide-constructors test.cpp
$ ./a.out
Reference
Move
Const reference

But, if I initialice v with a r-value:

vector<mystring> v{"hello"};

the output is:

$ g++ -std=c++11 -fno-elide-constructors test.cpp
$ ./a.out
Const reference

that means, no copies. With a r-value:

vector <mystring> v{mystring()};

output:

$ g++ -std=c++11 -fno-elide-constructors test.cpp
$ ./a.out
Move
Move
Const reference

I don’t understand two things:

  • Why with no-raw strings a second move (before the first copy/move) is performed?
  • Why with raw strings, no copies of “mystring” are performed?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:49:03+00:00Added an answer on June 16, 2026 at 3:49 am

    N.B. raw strings means something different in C++11, I think you mean string literal if you’re talking about "hello"

    • Why with raw strings, no copies of “mystring” are performed?

    Eh? There’s one copy, that’s why it prints Const Reference.

    vector<mystring> v{"hello"};
    

    The braced-init-list initializes the std::initializer_list<mystring> constructor parameter with a single element, which is constructed using the mystring(const char* c) constructor, then that element is copied into the vector using the mystring(const mystring&) constructor, which prints Const Reference. Pretty simple.

    • Why with no-raw strings a second move (before the first copy/move) is performed?

    How can the second move be before the first move?! 🙂

    It is an implementation detail of how the initializer_list is constructed, which normally wouldn’t matter because the extra moves would be elided.

    vector <mystring> v{mystring()};
    

    This creates a temporary mystring using the default constructor, then creates std::initializer_list<mystring> with a single element, which is constructed using the mystring(mystring&&) constructor with the temporary as its argument. That move construction prints Move. There’s another move done internally, printing Move again. Then the initializer_list element is copied into the vector as before, printing Const Reference.

    To answer your comment:

    The extra move you see also happens in the first case, but in that case the type being moved is const char* so it doesn’t print Move i.e. the statement std::vector<mystring>{ expr }; can be thought of as:

    auto tmp = expr;
    mystring tmparray[] = { std::move(tmp) };
    std::initializer_list<mystring> init_list( tmparray, 1 };
    std::vector<mystring> v(init_list);
    

    When expr is a (which is a non-const lvalue) creating tmp prints Reference then creating tmparray prints Movethen copying tmparray[0] into the vector prints Const Reference.

    when expr is "hello" creating the elements of tmp doesn’t print anything, and creating tmparray calls mystring(const char*) which doesn’t print anything, then copying tmparray[0] into the vector prints Const Reference.

    When expr is mystring() creating tmp prints Move then creating tmparray prints Move then copying tmparray[0] into the vector prints Const Reference

    With a two element braced-init-list like { expr1, expr2 } it can be though of as:

    auto tmp1 = expr1;
    auto tmp2 = expr2;
    mystring tmparray[] = { std::move(tmp1), std::move(tmp2) };
    std::initializer_list<mystring> init_list( tmparray, 2 };
    std::vector<mystring> v(init_list);
    

    So if you use { "hello", mystring() } you get Move printed once by tmp2 and Move printed once by tmparray and Const Reference printed twice by the elements of v.

    Of course normally all of this would be elided away, so there are no unnecessary copies or moves. -fno-elide-constructors is not really useful except for finding out what would happen, but doesn’t actually happen!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the sample: #include <iostream> using namespace std; class B { public: virtual void
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
I have a sample program in a file called helloworld.cpp: #include <iostream> using namespace
I have a test sample about coherence lock-unlock mechanism like this: public class Test
I have: car.cc #include car.h #include <iostream> using namespace std; extern C Car* create_object()
I have this simple program: // Include libraries #include <iostream> #include <string> #include <vector>
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
Possible Duplicate: Why does this simple std::thread example not work? Code: #include <iostream> #include
So I am trying to compile this simple code: // In Test.h #include <iostream>
This very simple code gives me tons of errors: #include <iostream> #include <string> int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.