Thread: Templates not initializing themselves each time

  1. #1
    Registered User
    Join Date
    Sep 2002

    Templates not initializing themselves each time

    	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);
    		//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:

    int main()
    	any blah = 1;
    	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;
    //	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:

    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?

    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?

  2. #2
    Registered User
    Join Date
    Sep 2002
    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;
    and now it works perfectly! I don't know why though.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    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!

    "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
    Yeah, they have a ton of template related bugs.

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

    Its double good though... more useful


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-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM