Fix for Bugzilla 2073

This commit is contained in:
Andrei Alexandrescu 2010-09-26 04:35:12 +00:00
parent b41834dde6
commit 5a64b47d10
2 changed files with 34 additions and 7 deletions

View file

@ -2127,6 +2127,13 @@ template ImplicitConversionTargets(T)
ImplicitConversionTargets; ImplicitConversionTargets;
else static if(is(T : Object)) else static if(is(T : Object))
alias TransitiveBaseTypeTuple!(T) ImplicitConversionTargets; alias TransitiveBaseTypeTuple!(T) ImplicitConversionTargets;
// @@@BUG@@@ this should work
// else static if (isDynamicArray!T && !is(typeof(T.init[0]) == const))
// alias TypeTuple!(const(typeof(T.init[0]))[]) ImplicitConversionTargets;
else static if (is(T == char[]))
alias TypeTuple!(const(char)[]) ImplicitConversionTargets;
else static if (isDynamicArray!T && !is(typeof(T.init[0]) == const))
alias TypeTuple!(const(typeof(T.init[0]))[]) ImplicitConversionTargets;
else static if (is(T : void*)) else static if (is(T : void*))
alias TypeTuple!(void*) ImplicitConversionTargets; alias TypeTuple!(void*) ImplicitConversionTargets;
else else

View file

@ -64,11 +64,11 @@
*/ */
module std.variant; module std.variant;
import std.traits, std.c.string, std.typetuple, std.conv; import std.traits, std.c.string, std.typetuple, std.conv, std.exception;
version(unittest) // version(unittest)
{ // {
import std.exception, std.stdio; import std.exception, std.stdio;
} //}
private template maxSize(T...) private template maxSize(T...)
{ {
@ -251,7 +251,7 @@ private:
// by the incoming TypeInfo // by the incoming TypeInfo
static bool tryPutting(A* src, TypeInfo targetType, void* target) static bool tryPutting(A* src, TypeInfo targetType, void* target)
{ {
alias TypeTuple!(A, ImplicitConversionTargets!(A)) AllTypes; alias TypeTuple!(A, ImplicitConversionTargets!A) AllTypes;
foreach (T ; AllTypes) foreach (T ; AllTypes)
{ {
if (targetType != typeid(T) && if (targetType != typeid(T) &&
@ -662,9 +662,22 @@ public:
T coerce(T)() T coerce(T)()
{ {
static if (isNumeric!(T)) static if (isNumeric!(T))
{
if (convertsTo!real())
{ {
// maybe optimize this fella; handle ints separately // maybe optimize this fella; handle ints separately
return to!(T)(get!(real)); return to!T(get!real);
}
else if (convertsTo!(const(char)[]))
{
return to!T(get!(const(char)[]));
}
else
{
enforce(false, text("Type ", type(), " does not convert to ",
typeid(T)));
assert(0);
}
} }
else static if (is(T : Object)) else static if (is(T : Object))
{ {
@ -1396,3 +1409,10 @@ unittest
} }
assert(j == 4); assert(j == 4);
} }
// test convertibility
unittest
{
auto v = Variant("abc".dup);
assert(v.convertsTo!(char[]));
}