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;
else static if(is(T : Object))
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*))
alias TypeTuple!(void*) ImplicitConversionTargets;
else

View file

@ -64,11 +64,11 @@
*/
module std.variant;
import std.traits, std.c.string, std.typetuple, std.conv;
version(unittest)
{
import std.traits, std.c.string, std.typetuple, std.conv, std.exception;
// version(unittest)
// {
import std.exception, std.stdio;
}
//}
private template maxSize(T...)
{
@ -251,7 +251,7 @@ private:
// by the incoming TypeInfo
static bool tryPutting(A* src, TypeInfo targetType, void* target)
{
alias TypeTuple!(A, ImplicitConversionTargets!(A)) AllTypes;
alias TypeTuple!(A, ImplicitConversionTargets!A) AllTypes;
foreach (T ; AllTypes)
{
if (targetType != typeid(T) &&
@ -663,8 +663,21 @@ public:
{
static if (isNumeric!(T))
{
// maybe optimize this fella; handle ints separately
return to!(T)(get!(real));
if (convertsTo!real())
{
// maybe optimize this fella; handle ints separately
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))
{
@ -1396,3 +1409,10 @@ unittest
}
assert(j == 4);
}
// test convertibility
unittest
{
auto v = Variant("abc".dup);
assert(v.convertsTo!(char[]));
}