Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2005-06-16 04:43:34


> I'm trying to find a way to see if a given object is convertible to
> std::string, but with certain restrictions. Normally, is_convertible<>
> would
> be fine, but in this case it doesn't work for my needs.
>
> Here's the situation in more detail. I've got a class Foo with a string
> conversion operator:
>
> class Foo { public: operator std::string () const; };
>
> I want some kind of is_convertible<Foo, std::string> to return FALSE, but
> something like is_convertible<char*, std::string> to return TRUE.
> Basically,
> I want is_convertible to check to see if the target type (std::string) has
> an implicit constructor taking the input type; I *don't* want
> is_convertible
> to check to see if the input type has a conversion-to-target-type
> function.
>
> Is there a way to do this?

Yes, it's quite cunning and we use it to implement is_enum: a conversion
sequence is only permitted one user defined conversion, so try:

is_convertible<your_type, convertible_to_string>

where convertible_to_string is a class with a constructor that accepts a
string as a single argument.

I think that will do it, I hope so anyway!

John.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net