Thread: Templated operator overload WITH a specific parameter type

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

    Templated operator overload WITH a specific parameter type

    I'm having trouble doing some overloads.
    It's trying to run this on every instance of the << or >> operators... I ONLY want it to do that if they contain the type any, explicitly... I don't want it to run the constructor!

    Be prepared for a lot of code:


    Code:
    template <any, typename UnknownType>
    	any operator <<(const any & lhs, const UnknownType & rhs)
    	{
    		any result;
    
    		if (lhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = any_cast<UnknownType>(lhs) << rhs;
    
    		return result;   // returns a copy of result
    	}
    Code:
    	template <typename UnknownType>
    	UnknownType operator <<(const UnknownType & lhs, const any & rhs)
    	{
    		UnknownType result;
    
    		if (rhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = lhs << any_cast<UnknownType>(rhs);
    
    		return result;   // returns a copy of result
    	}
    Code:
    	template <any, typename UnknownType>
    	any operator >>(const any & lhs, const UnknownType & rhs)
    	{
    		any result;
    
    		if (lhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = any_cast<UnknownType>(lhs) >> rhs;
    
    		return result;   // returns a copy of result
    	}
    Code:
    	template <typename UnknownType>
    	UnknownType operator >>(const UnknownType & lhs, const any & rhs)
    	{
    		UnknownType result;
    
    		if (rhs.type() == typeid(UnknownType))	// check that types are same.
    												// don't use float or double!
    			result = lhs >> any_cast<UnknownType>(rhs);
    
    		return result;   // returns a copy of result
    	}
    Its having trouble figuring out which overloads to do.
    I believe this is because it tries to do the one with UnknownType as parameter 1 instead of the ostream/istream...

    What am I doing wrong?
    Last edited by Trauts; 05-18-2003 at 05:33 PM.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Help specifying a specific size type in C++
    By indigo0086 in forum C++ Programming
    Replies: 23
    Last Post: 06-12-2007, 10:51 AM
  3. Question about creating a specific child window type
    By PJYelton in forum Windows Programming
    Replies: 8
    Last Post: 03-28-2005, 10:58 PM
  4. Inheriting from specific templated version of class
    By skiingwiz in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2004, 09:21 AM
  5. Need help reading in specific type of file
    By Natase in forum C Programming
    Replies: 4
    Last Post: 09-12-2001, 08:02 PM