Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 27, 2015 About variant | ||||
---|---|---|---|---|
| ||||
Dear that do a lot time wehere I not used std.variant. i would like to hide extra cast from user by using a generic ctor import std.variant; struct Alpha { Variant something; this(T)(T v){ something = cast(Variant)v; } } void main(){ auto a = Alpha!(int)( 6); auto b = Alpha!(string)( "hello"); auto l = new Alpha[](2); l[0] = a; l[1] = b; } but that do not works. Someone know a trick? thanks |
January 27, 2015 Re: About variant | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | I can do this import std.variant; struct Alpha { Variant something; this(Variant v){ something = v; } static Alpha build(T)(T v){ return Alpha( cast(Variant)v ); } } void main(){ auto a = Alpha.build!(int)( 6); auto b = Alpha.build!(string)( "hello"); auto l = new Alpha[](2); l[0] = a; l[1] = b; } If someone has better |
January 27, 2015 Re: About variant | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | On Tue, 27 Jan 2015 20:46:59 +0000, bioinfornatics wrote: > void main(){ > auto a = Alpha!(int)( 6); > auto b = Alpha!(string)( "hello"); The Alpha struct is not a template, only the constructor is. Remove the explicit instantiations and IFTI does the work: > void main(){ > auto a = Alpha( 6); > auto b = Alpha( "hello"); |
January 27, 2015 Re: About variant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Tuesday, 27 January 2015 at 21:00:16 UTC, Justin Whear wrote:
> On Tue, 27 Jan 2015 20:46:59 +0000, bioinfornatics wrote:
>
>> void main(){
>> auto a = Alpha!(int)( 6);
>> auto b = Alpha!(string)( "hello");
>
> The Alpha struct is not a template, only the constructor is. Remove the
> explicit instantiations and IFTI does the work:
>> void main(){
>> auto a = Alpha( 6);
>> auto b = Alpha( "hello");
Oh really cool
|
January 27, 2015 Re: About variant | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics Attachments: | On Tue, 27 Jan 2015 21:55:37 +0000, bioinfornatics wrote:
> On Tuesday, 27 January 2015 at 21:00:16 UTC, Justin Whear wrote:
>> On Tue, 27 Jan 2015 20:46:59 +0000, bioinfornatics wrote:
>>
>>> void main(){
>>> auto a = Alpha!(int)( 6);
>>> auto b = Alpha!(string)( "hello");
>>
>> The Alpha struct is not a template, only the constructor is. Remove the explicit instantiations and IFTI does the work:
>>> void main(){
>>> auto a = Alpha( 6);
>>> auto b = Alpha( "hello");
>
> Oh really cool
or this:
import std.variant;
struct Alpha {
Variant something;
this(T) (T v) {
something = cast(Variant)v;
}
}
void main () {
Alpha a = 6;
Alpha b = "hello";
}
|
Copyright © 1999-2021 by the D Language Foundation