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

  • Home
  • SEARCH
  • 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 8916021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:07:40+00:00 2026-06-15T05:07:40+00:00

I have some problems understanding when and if the move constructor or move assignment

  • 0

I have some problems understanding when and if the move constructor or move assignment operator are invoked, in particular in the context of a class with constant data member.
Consider the class

template<typename T> class A {
  const*T const P ;   // constant data member
  explicit A(const*T p) : P(p) { std::cerr<<" ctor: P="<<P<<'\n'; }
  void test() const { std::cerr" test: P="<<P<<'\n'; }
  // move and copy constructors and assignment operators here
};

and the test program

class B {
  int X[100];
  A<B> get_a() const { return A<B>(this); }
};

int main() {
  B b;
  A<B> a = b.get_a();   // which operator/ctor is used for '=' here?
  a.test();
}

then the results for compilation are different depending on the definitions provided for the move constructor and move assignment operator in class A<>, but also on compiler.

1 without any further declaration in class A<> (as above), both g++ (4.7.0) and icpc (13.0.1) compile fine (with option -std=c++11) and produce the expected output

ctor: P=0x7fffffffd480
test: P=0x7fffffffd480

2 if I declare

A&A::operator=(A&&) = delete;
A&A::operator=(const A&) = delete;

(which seems sensible in view of the constant data member which must be initialiser-list initialised), but don’t provide any further ctor, compilation fails with g++ but is okay wich icpc. If in addition I define either (or both) of

A::A(A&&) = default;
A::A(const A&) = default;

both compilers are happy. However, g++ is not happy with the combination

A::A(A&&) = delete;
A::A(const A&) = default;

while icpc is happy.

3 If I play the same game as in 2, except that A::A(A&&) = default; is replaced by

A::A(A&&a) : P(a.P) { std::cerr<<" move ctor: P="<<P<<'\n'; } // never called?

(and equivalent for A::A(const A&)), the results are completely identical, in particular no output is generated from these explicit move and copy ctors.

So which operator is used for = in main()? (and why is no output produced in the last test?)

And why is this operation allowed here at all, given that A<> has a constant data member (the results are identical if I replace the member const*T const P; with const T&R)?

Finally, in case of the different behaviours of g++ and icpc, which, if any, is correct?

  • 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-15T05:07:42+00:00Added an answer on June 15, 2026 at 5:07 am
    A<B> a = b.get_a();
    

    is not an assignment, but an initialization of a from an rvalue. This syntax should fail under C++0x if

    1. the move constructor is deleted,
    2. the move constructor is declared explicit,
    3. the copy constructor is deleted and no move constructor is defined,
    4. no move constructor is defined and, at the same time the move-assignment is defined or deleted.

    Declaration or deletion of the copy assignment operator should not have any influence.

    Correction: Different to the copy constructor (which is synthesized even if a user-defined copy assignment operator is provided), the compiler does not synthesize a move constructor if a user-defined move assignment is defined. Hence, the above list should be amended by 4 (which I have done now).

    Hence, in my opinion,

    • in [1] in the question, both compilers behave correctly,
    • in [2]/1, gcc behaves correctly (move assignment prevents generation of move constructor), icpc is wrong,
    • in [2]/2, both compilers are correct,
    • in [2]/3, gcc is correct, since the move constructor is explicitly deleted; icpc is wrong,
    • [3] is a mystery to me. Are you sure you’re correct?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to JPA and have some problems in understanding bidirectional relations. I
I have some problems with OpenCV s cvCanny(...) and the Image data types it
I have some problems with the understanding of casting buttons, maybe you can help
i have some problems on understanding gdb. i have a main function, i wrote
I have some problems with understanding the kernels for non-linear SVM. First what I
I'm having some problems using / understanding is_dir. (Yes, I have read the PHP
I have some problems with understanding the difference between soap:address and http:address tags in
I have some problems understanding a way to get the correct result from my
I have some problems understanding how the 'repair button' works in burn. If my
I have some understanding problems regarding the stringstream . Example: stringstream stream(commands); while (true)

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.