Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 08, 2015 Template constructor in a non-template struct. | ||||
---|---|---|---|---|
| ||||
Hi, I couldn't find information about problem I'm having. This builds: enum { Option1, Option2 } struct boring { this(int Opt = Option1)(int arg1, int arg2) { ... } } However, I can't figure out how to instantiate that constructor. When I call it like this: auto a = boring(1, 2); The compiler doesn't seem the template constructor, only the non-template constructors which take a different set of arguments. When I call it like this: auto a = boring!(Option1)(1, 2); It complains that my struct isn't a template struct, which makes sense; however, I don't really understand how I'd differentiate a constructor template from a class/struct template. Any ideas? -Chris |
February 08, 2015 Re: Template constructor in a non-template struct. | ||||
---|---|---|---|---|
| ||||
Posted in reply to ChrisG | ChrisG:
> I don't really understand how I'd differentiate a constructor template from a class/struct template.
One solution is to use a template struct (struct/class names start with an upper case in D, while typed enum members usually start with a lower case):
enum E { option1, option2 }
struct Boring(E Opt = E.option1) {
this(int arg1, int arg2) {}
}
void main() {
auto a = Boring!(E.option2)(1, 2);
}
If you want to instantiate the constructor template without type inference, then this seems to work, but it's not idiomatic D:
enum E { option1, option2 }
struct Boring {
this(E Opt = E.option1)(int arg1, int arg2) {}
}
void main() {
auto a = Boring().__ctor!(E.option2)(1, 2);
}
Bye,
bearophile
|
February 08, 2015 Re: Template constructor in a non-template struct. | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Thanks bearophile. Your first suggestion about making the struct a template is something I considered. However, because of all the code I've already written that approach would force me to use inheritance or convert a ton of functions to templates. Ick.
The __ctor syntax looks like the answer to my question. If my struct is used by anyone else though, I'm not sure it would be OK to assume they would even be aware of that syntax. hmmm... Thinking I'll just make it a runtime parameter for now.
Thank you.
-Chris
On Sunday, 8 February 2015 at 21:58:40 UTC, bearophile wrote:
> ChrisG:
>
>> I don't really understand how I'd differentiate a constructor template from a class/struct template.
>
> One solution is to use a template struct (struct/class names start with an upper case in D, while typed enum members usually start with a lower case):
>
>
> enum E { option1, option2 }
>
> struct Boring(E Opt = E.option1) {
> this(int arg1, int arg2) {}
> }
>
> void main() {
> auto a = Boring!(E.option2)(1, 2);
> }
>
>
> If you want to instantiate the constructor template without type inference, then this seems to work, but it's not idiomatic D:
>
>
> enum E { option1, option2 }
>
> struct Boring {
> this(E Opt = E.option1)(int arg1, int arg2) {}
> }
>
> void main() {
> auto a = Boring().__ctor!(E.option2)(1, 2);
> }
>
>
> Bye,
> bearophile
|
February 09, 2015 Re: Template constructor in a non-template struct. | ||||
---|---|---|---|---|
| ||||
Posted in reply to ChrisG | On Sunday, 8 February 2015 at 22:19:35 UTC, ChrisG wrote:
> Thanks bearophile. Your first suggestion about making the struct a template is something I considered. However, because of all the code I've already written that approach would force me to use inheritance or convert a ton of functions to templates. Ick.
>
> The __ctor syntax looks like the answer to my question. If my struct is used by anyone else though, I'm not sure it would be OK to assume they would even be aware of that syntax. hmmm... Thinking I'll just make it a runtime parameter for now.
>
You could also hide the ugly __ctor() call behind a nicely named factory method.
|
February 15, 2015 Re: Template constructor in a non-template struct. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Monday, 9 February 2015 at 09:52:13 UTC, Marc Schütz wrote:
>
> You could also hide the ugly __ctor() call behind a nicely named factory method.
Yes, that's pretty much what I ended up doing. Added a couple static member functions like:
static Boring CreateWithOption1(args);
static Boring CreateWithOption2(args);
Then made the struct constructor private. Not too bad for now. Thanks for your help.
|
Copyright © 1999-2021 by the D Language Foundation