Thread: Templates AND operator overloads

  1. #1
    Registered User
    Join Date
    Sep 2002

    Templates AND operator overloads

    I'm trying to use a template with an assignment operator overload.

    I want to do it function style, not class template or there is no point.

    Also, I think it is trying to use the default assignment operator...

    If I move the mouse cursor over a variable, it doesn't do anything, but if I comment out the template line, it recognizes a variable but won't compile unless I remove teh references to the template.

    It never runs my overload, I know that because if I have assert(1<0);, it won't crash. If I tell it to cout something, it won't work either.

    Here's a simplified version of my code.

    #include <iostream.h>
    //#include <string>
    class wimpyclass
    	void *data;
    	int getdata();
    	template <class unknowndatatype>
    	const wimpyclass & operator = ( const unknowndatatype & rhs );     // assign unknowndatatype
    	int x = 0;
    	data = &x;
    template <class unknowndatatype>
    const wimpyclass& wimpyclass::operator =(const unknowndatatype & rhs)
    //postcondition: normal assignment via copying has been performed
        if ( (data != (void*)&rhs) && (this != &rhs) )                             // check aliasing
            data = (void*)&rhs;										// assign the new void pointer data
    		//data = const_cast<void*>(&rhs);								// assign the new void pointer data
    		cerr << "\n\nCannot assign to itself\n\n";
        return *this;
    int wimpyclass::getdata()
    	return *(int*)&data;
    int main()
    	wimpyclass junk1;
    	junk1 = 50;
    	junk1 = 'a';
    	cout << junk1.getdata() << endl << *(int*)& << endl;
    	return 0;

    I have the void *data public so that I can make sure it is working... and it isn't assigning.

    However, if I remove the template and replace those with ints, it will work for ints.

    So is it possible to get a template to work with an operator overload?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Looks like a pretty cool MSVC bug!!

    I looked at the disassembly and the compiler doesn't even generate code for the assignments - debugger just skips right over em....

    I did find a work around though - you have to implement the method inline - within the class declaration. The compiler will then show you some errors in the operator=() code! As if your version doesn't even get compiled.

    Also, getdata() can be done a little easier by simply casting data to an int.

    You could also use "reinterpret_case<void*>()" for your void* casting just to be hip.

    Last edited by Codeplug; 04-08-2003 at 11:53 AM.

  3. #3
    Registered User
    Join Date
    Sep 2002
    if I remove the template part and change uknowndatatype to int, it works.t hanks tho

    Eh, sorry... I was using a different version than the one I posted

    My version worked, that one had errrors.
    Last edited by Trauts; 04-09-2003 at 08:54 PM.

Popular pages Recent additions subscribe to a feed
Website Security Test