• Forum
  • Lounge
  • What to do about midterm question profes

 
What to do about midterm question professor has wrong?

Pages: 12
So the question is to name all of the member functions called in a chunk of code. Something along these lines:

1
2
3
4
5
6
X fun(X &y) {
    X x = y;  //the copy constructor is used, 
                  //but the prof. thinks the assignment operator is used.  
                  //Not sure if he thinks that a default constructor is also used.
    return  x;
}


So I mentioned to him that my compiler replaces the line with a call to the copy constructor. The professor thinks this is an optimization. I was just wondering if anyone knows what the standard says.

This is actually from a practice midterm, and the real one is coming up, and I don't know wether or not I should answer incorrectly in order to get credit, or answer correctly and then petition it later, or approach the professor again about the question to figure out exactly what he thinks will happen, or to refer him to the standard.

It's just that I don't want to embarrass the professor, or create any awkwardness.

I guess I should need to ask for advice about this situation. These types of things just eat away at me because I absolutely hate correcting professors, but I also hate being expected to give an answer that I know is incorrect.
Last edited on
closed account (N36fSL3A)
Confront him alone, don't do it in front of the class. Don't bring any more shame to him than is necessary.

You shouldn't let him misinform.
Your professor is right. I'm not so sure....
I actually got burned on this recently as well.

(note the below may be wrong):

X x(y);
Would be calling the copy ctor.


X x = y;
Calls default ctor and copy assignment. Though most (all?) compilers will optimize this away to a copy ctor call... even in "debug" builds.


The subtle difference between the two can be exposed with the explicit ctor. If the copy ctor is explicit, X x(y); will succeed, but X x = y; will generate a compiler error.
(Edit: explicit example: http://ideone.com/IUVmXG )

Furthermore... if the copy assignment operator is private... X x(y); will succeed, but X x = y; will generate a compiler error (or it should -- apparently some versions of VS will still allow it).


EDIT: Actually... maybe I'm remembering this wrong... gimmie a minute to see if I can find that post...


EDIT again: This thread was the one:

http://www.cplusplus.com/forum/beginner/107622/
Last edited on
Wow, all this time it was an often-used optimization and not an actual language feature...
closed account (N36fSL3A)
What? I didn't know that. Wow...
Isn't C++ fun ;)
closed account (3qX21hU5)
@ Disch are you sure?

Here is a excerpt from Wiki that contradicts this and makes sense to me. Though I am by no means saying Wiki is a 100% unfailible resource I am just curious.

In the C++ programming language, the assignment operator, '=', is the operator used for assignment. Like most other operators in C++, it can be overloaded.

The copy assignment operator, often just called the "assignment operator", is a special case of assignment operator where the source (right-hand side) and destination (left-hand side) are of the same class type. It is one of the special member functions, which means that a default version of it is generated automatically by the compiler if the programmer does not declare one. The default version performs a memberwise copy, where each member is copied by its own copy assignment operator (which may also be programmer-declared or compiler-generated).

The copy assignment operator differs from the copy constructor in that it must clean up the data members of the assignment's target (and correctly handle self-assignment) whereas the copy constructor assigns values to uninitialized data members.[1] For example:


1
2
3
4
My_Array first;           // initialization by default constructor
My_Array second(first);   // initialization by copy constructor
My_Array third = first;   // Also initialization by copy constructor
second = third;           // assignment by copy assignment operator 
Last edited on
@ Disch are you sure?


Not really. I'm very fuzzy on the details. That other thread I linked explains it in more detail (though I edited in the link so you might not have seen it before you posted).

link again for reference: http://www.cplusplus.com/forum/beginner/107622/


It has to do with "direct initialization" vs. "copy initialization":

http://en.cppreference.com/w/cpp/language/direct_initialization
http://en.cppreference.com/w/cpp/language/copy_initialization


EDIT:

Actually as I read further... I don't really know any more. It sounds like it might be calling the copy ctor.

Grah these edge cases are confusing.


EDIT again (hopefully last one):

My understanding of this particular behavior is not clear enough, so I'll have to step out of this one and less someone else (Cubbi?) explain the finer details. I'm interested in this as well.
Last edited on
Awesome. I'm glad I asked. I wasn't actually sure if this was just a practically always used optimization or if it was part of the standard. Thanks for replying.
Looks like Disch is right:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

struct Test
{
	Test(Test const &) = delete;
	Test(Test &&) = delete;
	Test &operator=(Test const &) = delete;
	Test &operator=(Test &&) = delete;

	Test(int &x)
	{
		++x;
	}
};

int main()
{
	int x = 6;
	Test t = x;
	std::cout << x << std::endl;
}
prog.cpp: In function ‘int main()’:
prog.cpp:19:11: error: use of deleted function ‘Test::Test(Test&&)’
  Test t = x;
           ^
prog.cpp:6:2: error: declared here
  Test(Test &&) = delete;
  ^
http://ideone.com/ykv9tm
closed account (3qX21hU5)
less someone else (Cubbi?) explain the finer details


Heh I was just about to say the exact same thing. Cubbi has a amazing knowledge of the standard so would love to here his opinion on this.
It was my understanding that copy construction must be supported for copy initialization to be supported, although the standard doesn't require the copy constructor to be invoked for copy initialization. Unfortunately, I haven't time to look it up now. Off to work.
X x = y; //the copy constructor is used,
//but the prof. thinks the assignment operator is used.

This is clearly wrong; this is a declaration, not an expression, the equals sign is not an operator.

Since people want the "finer details" (and in case your prof is amenable to learning and is aware of the C++ standard), X x = y; is a declaration-statement ($6.7[stmt.dcl]), which consists of one simple-declaration ($7[dcl.dcl]/1), which consists of three parts: decl-specifier-seq X, init-declarator-list x=y, and the semicolon. The init-declarator-list, obviously, consists of a single init-declarator, which is composed of two parts: the declarator x and the initializer = y ($8[dcl.decl]/1). The initializer grammar is in $8.5[dcl.init], which tells us that the syntax of this particular declarator is the equals sign followed by initializer-clause (which happens to be an id-expression) y.
So, the equals sign is not an assignment operator, it is a punctuator in the declaration grammar.

As for what's executed here and why, yes, this is, grammatically, copy-initialization from a nameless temporary obtained by copy-initialization performed as part of lvalue to rvalue conversion (which is why Disch's testcase selects the move constructor: the argument is an rvalue), and of course every compiler since very early 90s optimized this into a single copy constructor call under the copy elision rules "when a temporary class object that has not been bound to a reference would be copied/moved to a class object with the same cv-unqualified type" (from §12.8[class.copy]/13)
Last edited on
Admittedly i didn't understand everything that cubbi said (thanks for the explanation by the way ). but i think over lining point was
1
2
3
4
5
6
X x(y); - copy constr

X x = y; - copy constr because it is in the declaration

X x;
x = y; - assignment operatior because it is an expression


am i sorta on the right rack?
YOu guys would love my professor, he actually studied physics and moved on to computer science later, If you proved my professor wrong in class he would love that, he would go and get a coffee and make you teach the class...true story.
ignore the profs they don't know anything :)

Btw, how does a computer science prof compare to a good programmer in one of the big companies?
Last edited on
@tition
At my university it seems most of the professors used to work for big companies, so I assume they're about the same.
They tend to be very smart people, and their focuses tend to be on their research interests. A lot of them lean more towards the mathematics or theoretical end of the spectrum then the programming end.

I think they typically don't spend as much time coding as they do writing on the board, working out equations, doing proofs, giving lectures, coordinating things, trying to get grants, etc. So you can't expect them to be as sharp a practical programmer as someone who codes all day almost every day.

My professor is very good, and a great teacher, even if he isn't always the best technical guru. His research is mostly in flow simulation such as jet streams. His group apparently is behind some of the modern flame graphics in video games as well. I have a ton of respect for him and the work his research group does.
Last edited on
Paoletti301 wrote:
Admittedly i didn't understand everything that cubbi said

Don't feel bad, the C++ standard (whose terms and paragraphs I quoted there) is written for the C++ compiler writers. It is difficult to follow, but it is the final authority on any language questions - when the compiler writers get it wrong, they get bug reports quoting those paragraphs. Even if the prof is aware that the standard exists, just saying "it's in the standard" is not sufficient (and is kind of rude). If you do the legwork first, you might be able to convince them (but definitely in private).

The main point to take away is that the symbol = in T x = y; is not related to assignment, just like the symbol * in int* x = NULL; is not related to multiplication. There aren't many printable ASCII characters that aren't letters or digits, and C++ reuses them in many different ways, depending on the context.
Why would you want to confront a professor in private?? Are they so full of pride and arrogance they can't admit they are human??
Pages: 12