Templates not initializing themselves each time

This is a discussion on Templates not initializing themselves each time within the C++ Programming forums, part of the General Programming Boards category; Code: template <typename OldType, typename NewType> any & change_type(any & operand) { cout << operand.type().name() << " OprType" << endl; ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Templates not initializing themselves each time

    Code:
    	template <typename OldType, typename NewType>
    	any & change_type(any & operand)
    	{
    		cout <<  operand.type().name() << " OprType" << endl;
    		cout << typeid(OldType).name() << " OldType" << endl;
    		cout << typeid(NewType).name() << " NewType" << endl;
    		//any::placeholder * newcontent = new any::holder<NewType>(void_cast<NewType>(any_cast<OldType>(operand)));
    		//any::placeholder * newcontent = new any::holder<NewType>(void_cast<NewType>(&static_cast<any::holder<OldType> *>(operand.content)->held));
    		OldType olddata = static_cast<any::holder<OldType> *>(operand.content)->held;//any_cast<OldType>(operand);
    		NewType newdata = void_cast<NewType,OldType>(olddata);
    		//std::swap(operand.content,newcontent);
    		//delete newcontent;
    		operand = newdata;
    		//any temp = void_cast<NewType>(any_cast<OldType>(operand));
    		//operand = temp;
    		return operand;
    	}
    There's a ton of commented out stuff... that was all sorts of other stuff I tried.

    Here's my main:

    Code:
    int main()
    {
    	any blah = 1;
    	change_type<int,char>(blah);
    	cout << any_cast<char>(blah) << endl; // now this will work without crashing!
    	cout << void_cast<char> (2) << ' ' << void_cast<int> (void_cast<char> (2)) << endl;
    
    
    	//	system("cls");
    
    	int *data1 = (int*)"abcdef";
    	any blah2 = data1;
    //	cout << blah2.type().name() << endl;
    	change_type<int*,char*>(blah2);
    
    //	cout << any_cast<char*>(blah2) << endl;
    
    return 0;
    }
    If I comment out the first change_type, it runs fine.

    If I comment out the second one instead, it runs fine.

    If you run both change_types, it crashes.

    I cannot figure it out and its really bugging me.

    Any ideas?

    And if you comment out the parts in the function where it changes things, and the part where it uses cout and any_cast in main, you get this for output:

    Code:
    int OprType
    int * OldType
    char * NewType
    ☻ 2
    int * OprType
    int * OldType
    char * NewType
    Press any key to continue
    Which is what it should be... except for the 2nd and 3rd line...

    However, those are correct when the second call of change_type is commented out. Does anyone know why its doing this?

    Code:
    operand = void_cast<NewType>(any_cast<OldType>(operand));
    has the same effect on it... it still isn't working. It doesn't seem to change the template argument the second time.


    I know the problem is that the any_cast is taking the wrong OldType... it seems to work the first time, and then not change later.

    I can give the exact problem now that I figured it out:

    The first time you call it with <int,char>, it works fine. But if you call it with <char,int> it'll crash because it is still trying to use <int,char>... why?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    This is weird... I changed the return type so now it looks like this:

    PHP Code:
    class any
    {
    // lots of code
    template <typename NewType>
            
    friend NewType change_type(any operand);
    // lots of code
    };

        
    template <typename NewType>
        
    NewType change_type(any operand)
        {
            
    NewType newdata = *void_cast<NewType*>(&static_cast<any::holder<void*> *>(operand.content)->held);
            
    operand newdata;
            return 
    newdata;
        } 
    and now it works perfectly! I don't know why though.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just thought I'd mention:
    VC++6 and 7 have a bug so that no constructor calls for global instances of templated classes might be issued.

    I had code that showed the bug but I deleted it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Yeah, they have a ton of template related bugs.

    But changing the return type fixed it... thats strange.

    Its double good though... more useful

    Thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-02-2006, 11:46 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21