How is `auto` and not `alias` appropriate for alias sequences?
ZombineDev via Digitalmars-d
digitalmars-d at puremagic.com
Fri Dec 11 06:12:16 PST 2015
On Friday, 11 December 2015 at 11:18:39 UTC, Shriramana Sharma
wrote:
> Hello. I just found that the following code compiles without
> any problem:
>
> struct Foo { int val; string name; }
> Foo foo = {1, "one"};
> auto t = foo.tupleof;
>
> Trying to use `alias` i.o. `auto` above fails.
>
> Now IIUC, trying to take the address of t fails, so it's still
> a compile- time-only construct without "real" i.e. runtime
> existence. The tuple is in fact an AliasSeq, no? In which case,
> I would have thought that `alias` (for compile-time symbols)
> and not `auto` (for runtime variables) would be the appropriate
> choice.
>
> Can someone please explain this situation? Thanks.
alias can refer to types, templates, template argument lists and
compile-time expressions. It can't refer to run-time variables.
auto can refer only to run-time values.
foo is run-time value.
foo.tupleof is also run-time value, like an object of some
template instance of std.typecons.Tuple.
The template arguments list of std.typecons.Tuple is a
compile-time sequence that you can put in AliasSeq.
typeof(foo.tupleof) is a type list to which you can only refer
with alias or use as template arguments for some template.
For example you can put typeof(foo.tupleof) in AliasSeq:
alias FooFieldTypes = AliasSeq!(typeof(foo.tupleof));
http://dpaste.dzfl.pl/decbc38e6e71
More information about the Digitalmars-d
mailing list