Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 23, 2013 template in struct | ||||
---|---|---|---|---|
| ||||
How to make workable something like this: struct S (T...) { T args; string arg_str; foreach(i; args) { arg_str ~ to!string(i); } } void some_function(S s) { // here ... Error: struct S(T...) is used as a type s.args = [a, b, c]; string args = s.arg_str; } |
March 23, 2013 Re: template in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to dsmith | On Saturday, 23 March 2013 at 17:08:27 UTC, dsmith wrote:
> How to make workable something like this:
>
> struct S (T...) {
> T args;
> string arg_str;
>
> foreach(i; args) {
> arg_str ~ to!string(i);
> }
> }
>
> void some_function(S s) { // here ... Error: struct S(T...) is used as a type
> s.args = [a, b, c];
> string args = s.arg_str;
> }
You must specify the template type of S.
|
March 23, 2013 Re: template in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to dsmith | On Saturday, 23 March 2013 at 17:08:27 UTC, dsmith wrote:
> How to make workable something like this:
>
> struct S (T...) {
> T args;
> string arg_str;
>
> foreach(i; args) {
> arg_str ~ to!string(i);
> }
> }
>
> void some_function(S s) { // here ... Error: struct S(T...) is used as a type
> s.args = [a, b, c];
> string args = s.arg_str;
> }
for a start, you have a foreach sitting in a struct declaration, not in any constructor or method.
I think what you mean is:
struct S(T...) {
T args
string arg_str;
this(T args) {
foreach(i; args)
arg_str ~ to!string(i);
}
}
if you want the arg_str to be updated every time args are changed, you'll need to introduce a method (or property) for this.
|
March 23, 2013 Re: template in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to dsmith | On Sat, Mar 23, 2013 at 6:08 PM, dsmith <dsmith@nomail.com> wrote: > How to make workable something like this: > > struct S (T...) { > T args; > string arg_str; > > foreach(i; args) { > arg_str ~ to!string(i); > } > } > > void some_function(S s) { // here ... Error: struct S(T...) is used as a > type > s.args = [a, b, c]; > string args = s.arg_str; > } S is not a type here, but a template name. The type is S!(SomeArgs). That's what you should pass to some_function: void some_function(Args...)(S!(Args) s) { s.args = [a, b, c]; string args = s.arg_str; } |
Copyright © 1999-2021 by the D Language Foundation