Re: Alternate way for creating an explicit bool-conversion operator?
On Nov 6, 9:43 am, "Gennaro Prota" <clcppm-pos...@this.is.invalid>
wrote:
On Sat, 05 Nov 2011 22:17:17 +0100, Daryle Walker <dary...@gmail.com>
wrote:
If a compiler doesn't yet support marking conversion-operator member
functions as explicit, can we mark off Boolean-without-Integer as:
class MyType
{
public:
//...
/*explicit*/ operator bool() const;
operator signed char() const = delete; // and no higher signed-
int types
operator unsigned char() const = delete; // and no higher
unsigned-int types
// maybe add "char" and "u8char" and any other lowest-level char
types...
operator float() const = delete; // and no higher floating-point
types
//...
};
But you are adding a lot of types explicitly, which Bjarne's
example doesn't. Why?
Instead of deleting conversoin operators for all bool-Convertibles,
consider this:
struct Testable
{
template <class T, class = typename
std::enable_if<std::is_convertible<bool, T>::value, T>::type>
operator T() = delete;
operator bool() { return "whatever"; }
};
Sadly, although imo this _should_ work according to the C++11
Standard, it did not on
the compilers on which I testet it:
- MSVC 2010 has not implemented default arguments on function
templates yet.
- On gcc-4.5.1 (http://www.ideone.com/AiYu8), the template conversion
operator either
does not take part in overload resolution, or the compiler prefers
operator bool
plus promotion over operator T<int>.
Regards,
Arne
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]