Is there a way to initialize a non-assigned structure declaration (or is it a definition)?
Nick Sabalausky
SeeWebsiteToContactMe at semitwist.com
Sat Nov 10 04:49:50 PST 2012
On Sat, 10 Nov 2012 00:35:05 +0100
"Too Embarrassed To Say" <kheaser at eapl.org> wrote:
>
> auto p3 = Parameterized!(int, double, bool, char)(57, 7.303,
> false, 'Z'); // compiles
> // but not
> // Parameterized!(int, double, bool, char)(93, 5.694, true, 'K')
> p4;
That's as expected. Variable declarations are of the form:
Type varName;
// or
Type varName = initialValue;
(In the second form, "auto" is optionally allowed to stand in for the
type.)
And struct literals (ie the actual values of a struct type) are of the
form:
Type(params)
So:
- Parameterized is a template
- Parameterized!(int, double, bool, char) is a type.
- Parameterized!(int, double, bool, char)(93, 5.694, true, 'K') is a
*value* of the above type, it's *not* a type.
So when you say:
Parameterized!(int, double, bool, char)(93, 5.694, true, 'K') p4;
That's a value, not a type. So that's just like saying:
5 myInt;
// or
"Hello" myStr;
Which doesn't make sense. What you wanted to say was:
int myInt = 5;
// or
auto myInt = 5;
// or
string myStr = "hello";
// or
auto myStr = "hello";
Therefore, you have to say:
auto p3 = Parameterized!(int, double, bool, char)(93, 5.694, true,
'K');
Because *that* is of the form: Type varName = initialValue;
If you want an easier way to do it, you can do this:
alias Parameterized!(int, double, bool, char) MyType;
auto p3 = MyType(93, 5.694, true, 'K')
Or, like Ali said, you can make a convenience function.
More information about the Digitalmars-d-learn
mailing list