Jump to content
  • Advertisement
Sign in to follow this  
Shnoutz

Templated = operator weirdness.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I got a small mystery here...
class Variable : protected Void
{
public:

	template < typename Value >
	Variable & operator=(Value const & p_value)
	{
		return *this;
	}
};

Variable v1 = "asdf"; // Calls the operator =

Variable v2;
v2 = v1; // Does not call operator =
Asking a collegue I got "why the *&#@#$ do you want that?". Asking another I got "you could create overloaded version of the operator". The last one is a way to bypass the problem... I want to understand.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Shnoutz
Variable v1 = "asdf"; // Calls the operator =
No it doesn't. It calls the constructor. It's likely that you've been slightly misinterpreting the source of the error... is that your actual code?

Share this post


Link to post
Share on other sites
For all C++ classes, there are certain functions that if you don't define yourself, the compiler will define for you. One of those is MyClass & operator=(const MyClass &). Since your class doesn't define that function, the compiler creates a version of its own. Since there are actually two versions of operator=() in your class, when you try assigning a variable, the compiler looks for the best match. An exact match with a non-template function beats out an exact match with a template function, so the compiler generated version is called rather than your template.

Share this post


Link to post
Share on other sites
Very cool, explains everything.

Where can I find a list of thoses functions/operators that are compiler generated?

Share this post


Link to post
Share on other sites
They are the default constructor, copy constructor, copy assignment operator and destructor. Though the default constructor is suppressed if any constructor is declared, not just the default constructor.

Share this post


Link to post
Share on other sites
Perhaps you can implement the assignment operator in terms of the template version:


#include <iostream>
struct A
{
template <class T>
const A& operator=(const T&)
{
std::cout << "Template version\n";
return *this;
}
const A& operator=(const A& other)
{
return operator=<>(other);
}
};

int main()
{
A a, b;
a = b;
}

Share this post


Link to post
Share on other sites
I'm on the "why the *&#@#$ do you want that?" side of the fence here. :) (Especially considering that the assignment operator doesn't appear to *do* anything, and you can only really tell what's called by using a debugger.) But yes, a template assignment operator does not provide a copy assignment operator; likewise, a template one-argument constructor does not provide a copy constructor. Sorry. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!