www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.conv.to can't convert to bool?

reply Andrej Mitrovic <none none.none> writes:
import std.stdio;
import std.conv : to;

void main()
{
    uint state = 1;
    writeln( to!bool(state) );
}

D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\conv.d(99): Error: template
std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) &&
isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) does not match any
function template declaration
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\conv.d(99): Error: template
std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) &&
isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) cannot deduce
template function from argument types !(bool)(uint)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\conv.d(99): Error: template
instance errors instantiating template
boolConv.d(9): Error: template instance std.conv.to!(bool).to!(uint) error
instantiating

What's the big problem with converting an int/uint to bool? I'm using a cast
for now.
Mar 27 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Wow not a minute later and I get bitten by my own solution. A C
function returned 1 for a supported feature, and -1 otherwise. And of
course -1 got converted to true, so then I had a bug in my code.

Damn silly C functions which return -1 when they should return 0.

Or damn me for not RTFM'ing.
Mar 27 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrej Mitrovic Wrote:

 Wow not a minute later and I get bitten by my own solution. A C
 function returned 1 for a supported feature, and -1 otherwise. And of
 course -1 got converted to true, so then I had a bug in my code.
 
 Damn silly C functions which return -1 when they should return 0.
 
 Or damn me for not RTFM'ing.
Yeah, so the reason it doesn't do that conversion is because it will bite you. Actually D already uses non-zero as true. Why do you need to cast it?
Mar 28 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
It can't implicitly convert an int to a bool. The C function returns an int.
Mar 28 2011