Thread: explicit

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

    explicit

    Is there a way to get a constructor to act explicit in only certain functions?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: explicit

    Originally posted by Trauts
    Is there a way to get a constructor to act explicit in only certain functions?
    No, not that I know of. I can't think of any reason why you would need to either; care to enlighten me?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    my "any" type has a constructor that takes any type.

    This is a pain because of overloads:

    Code:
    template <typename UnknownType>
    	bool operator !=(const any & lhs, const UnknownType & rhs)
    	{
    Gives you an error saying that it cannot choose between different overloads. (i.e. it tries to treat an int as an any...)

    Making the constructor explicit works, but then there's this problem:

    Code:
    any x;
    x = 5;
    needs to be

    Code:
    any x;
    x = (any)x;
    Last edited by Trauts; 05-16-2003 at 07:34 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I think I found a way to do it...

    instead of

    Code:
    	template <typename UnknownType>
    	bool operator !=(const any & lhs, const UnknownType & rhs)
    	{
    		if (lhs.type() == typeid(UnknownType))	// check that types are same.
    												// no support for compatible but not same,
    												// sorry. Too much work to check for
    												// int and float, etc.
    			return any_cast<UnknownType>(lhs) != rhs;
    
    		return false;   // returns a copy of result
    	}
    I tried

    Code:
    	template <any, typename UnknownType>
    	bool operator !=(const any & lhs, const UnknownType & rhs)
    	{
    		if (lhs.type() == typeid(UnknownType))	// check that types are same.
    												// no support for compatible but not same,
    												// sorry. Too much work to check for
    												// int and float, etc.
    			return any_cast<UnknownType>(lhs) != rhs;
    
    		return false;   // returns a copy of result
    	}

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    That worked on all of the overloads except the stream ones...

    How might I fix this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interesting behavior with explicit copy constructor
    By Clairvoyant1332 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2008, 03:19 PM
  2. Replies: 6
    Last Post: 08-12-2007, 01:02 PM
  3. Explicit keyword
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2006, 06:43 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Implicit and Explicit
    By ripper079 in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2002, 12:22 PM