# Unexpected program flow (templates / conversion operator).

This topic is 2077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Experiencing unexpected program flow.

Minimal testcase follows:
class Obj {
public:
Obj() { LOG(L"new Obj"); }
~Obj() { LOG(L"del Obj"); }
};

template<class T, template<class> class W> class Emit {
public:
Emit(T* ref) : m_ref(ref) { LOG(L"new Emit"); }
W<T> get() { LOG(L"get from Emit"); return W<T>(m_ref); }
T* m_ref;
};

template<class T> class Wrap {
public:
Wrap() : m_ref(0) { LOG(L"new Wrap(0)"); }
Wrap(T* ref) : m_ref(ref) { LOG(L"new Wrap(T*)"); }
Wrap(Wrap<T>& ref) : m_ref(&ref) { LOG(L"new Wrap(Wrap<T>&)"); ref.m_ref = 0; }
~Wrap() { LOG(L"del Wrap : %p",m_ref); }

operator T*() const { LOG(L"Wrap to T*"); return m_ref; }

Wrap<T>& operator=(T* ref) { LOG(L"Wrap = T*"); m_ref = ref; return *this; } // this will be used, incorrectly, when i use assignment instead
Wrap<T>& operator=(Wrap<T>& ref) { LOG(L"Wrap = Wrap<T>&"); m_ref = ref.m_ref; ref.m_ref = 0; return *this; }

T* m_ref;
};

void wtf() {
Emit<Obj, Wrap> emit(new Obj());
Wrap<Obj> wrap = emit.get();
}

expecting:
new Obj
new Emit
get from Emit
new Wrap(T*)        - first formed in Emit::get
new Wrap(Wrap<T>&)  - copy constructor (return value optimization removes the second one)
del Wrap : 00000000 - delete temporary object ... content has been transferred
del Wrap : 006F6580 - "wrap" goes out of scope

but actually get:
new Obj
new Emit
get from Emit
new Wrap(T*)        - first formed in Emit::get
Wrap to T*          - have to downcast/unwrap to use the wrong copy constructor
new Wrap(T*)        - using the wrong copy constructor
del Wrap : 006F6580 - delete temporary object ... this is bananas
Wrap to T*               ???
new Wrap(T*)             ??? going bananas twice, presumably first at "Wrap<Obj> get() { return Wrap<Obj>(ref); }" and then "Wrap<Obj> = emit.get();"
del Wrap : 006F6580      ??? given that it goes bananas - no return value optimization is done with Debug nor Release target.
del Wrap : 006F6580 - "wrap" goes out of scope

What am i missing? Can i force it to be sensible?

##### Share on other sites

Make Wrap(T* ref) explicit.

Otherwise the compiler will prefer it over a non-const copy constructor.

##### Share on other sites
Did not think of "explicit" - nice. Now return value optimization is in action and actually removes even more than i hoped for .
new Obj
new Emit
get from Emit
new Wrap(T*)
del Wrap : 00556580
However, one problem remains - assignment still goes to places:
new Obj
new Emit
new Wrap(0)
get from Emit
new Wrap(T*)
Wrap to T*      - bananas
Wrap = T*
del Wrap : 005565C0
del Wrap : 005565C0
Is there something i could do to make it prefer the templated one?

edit: FFS, gamedev, fix the god-damn forum software - i have been around here ~10 years and i can not remember any time any of the forum software variations have ever worked fully. It is not even funny anymore.

##### Share on other sites

You probably want the copy constructor and the assignment operator to take the parameter by const reference instead of by reference. For example, the return statement is Obj::get() takes an unexepected route because it cannot copy by the copy constructur; it would pass a temporary value to a function taking a non-const reference which is not legal.

The same problem applies to the assignment operator; the return value from Obj::get() is a temporary and cannot be passed to the assignment operator taking a non-const reference. But your assignment operator is behaving in an unexpected way anyway; it modifies the right-hand-side (sets ref.m_ref to zero) which is not the expected behavior of a copy-assignment. What you have implemented is the move-assignment operator disguised as the copy-assignment operator.

##### Share on other sites

Can't upvote Brother Bob's post enough.

For example, the return statement is Obj::get() takes an unexepected route because it cannot copy by the copy constructur; it would pass a temporary value to a function taking a non-const reference which is not legal.

To expand on this a little bit:

// Invalid example: void foo(int& x) {     std::cout << x << std::endl; }   foo(7 * 6); // not legal; can't bind a temporary to a non-const reference (because what if foo() changed it?)   // Valid example void foo(const int& x) {     std::cout << x << std::endl; }   foo(7 * 6); // legal; the temporary is bound to a const reference, so the compiler doesn't have to worry about foo() modifying it

##### Share on other sites
1) You probably want the copy constructor and the assignment operator to take the parameter by const reference instead of by reference. For example, the return statement is Emit::get() takes an unexepected route because it cannot copy by the copy constructur; it would pass a temporary value to a function taking a non-const reference which is not legal.

2) The same problem applies to the assignment operator; the return value from Obj::get() is a temporary and cannot be passed to the assignment operator taking a non-const reference.

3) But your assignment operator is behaving in an unexpected way anyway; it modifies the right-hand-side (sets ref.m_ref to zero) which is not the expected behavior of a copy-assignment. What you have implemented is the move-assignment operator disguised as the copy-assignment operator.
Maybe it is getting late or i have language barrier problems, but i do not understand what was said. :/

1) I assume by "const reference" (makes no sense to me and pretty sure VC wont even allow it) you mean actually "reference to const" in which case - no, in my case. Source cannot be const (look it like a tape from old days - the copy is not a perfect clone and the source suffered some wear and is not what it used to be either).

"it would pass a temporary value to a function taking a non-const reference to non-const which is not legal" - why? Can you give some example of what you mean?

2) Why not? (assuming reference to non-const was meant)

3) Unusual, perhaps, but perfectly reasonable. It is not copy-assignment nor move-assignment (it only looks like move assignment because it is a minimized example-case) - it is just an assignment that does stuff that is expected to happen when one uses assignment with the relevant object types.

##### Share on other sites
@Cornstalks: thanks for the example (why do you refer to "reference to non-const" as "non-const reference"?) - trying to understand it now.

##### Share on other sites
3) Unusual, perhaps, but perfectly reasonable. It is not copy-assignment nor move-assignment (it only looks like move assignment because it is a minimized example-case) - it is just an assignment that does stuff that is expected to happen when one uses assignment with the relevant object types.

If you try to force the language to do the unexpected, like having something that looks like like it might be an assignment operator not have the signature of an assignment operator and not work like an assignment operator, you should really expect your program to do the unexpected.

Also, there is a significant difference between a const reference and a reference to a const.  One is "int const&" the other is "int& const"...  can you guess which is which, and which one "const int&" is?

##### Share on other sites
@Cornstalks: thanks for the example (why do you refer to "reference to non-const" as "non-const reference"?) - trying to understand it now.

Because that's how the rest of the world typically does it (at least in English). I guess you could technically call it a misnomer, maybe, but references themselves are always const (so it makes no sense to have a truly "non-const reference" (that is, a reference that can be changed to refer to another object; it's impossible in C++)), so when people talk about (non) const references, the (non) const always refers to the object that the reference refers to, not to the reference itself (since the reference itself is always const).

Edited by Cornstalks

##### Share on other sites
You could drop the = T* operator and allow assignment only from another Wrap<T>.

But yes, this type of thing can make code behave in unexpected ways. Even std::auto_ptr is deprecated for that reason.

1. 1
2. 2
3. 3
Rutin
19
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632628
• Total Posts
3007518
• ### Who's Online (See full list)

There are no registered users currently online

×