Thread: Another weird problem

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

    Another weird problem

    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?
    Last edited by Trauts; 05-05-2003 at 04:19 PM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    here are the files

    Edit:

    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.
    Last edited by Trauts; 05-05-2003 at 06:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Problem With Pointer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 07:50 PM
  2. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  5. Weird class problem!
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 06:12 AM