www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - variables of type void & optional members

reply "Ilya Zaitseff" <sark7 mail333.com> writes:
Now following not compiles - "variable optional_member voids have no value"

struct Foo(T)
{
   T optional_member;
   int required_member;
}

void main()
{
   alias Foo!(void) Foo1;
}

But why D don't allow void variables when no one use it? If I don't need  
an optional_member in Foo, why waste space for fictional helper type?
I think, D should give an error, when someone try to _use_ void variable.

Just my opinion, anyway :)
Sep 13 2004
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
if you're trying to do padding in the struct, you can use other types, or
the align() .. thing.

void itself has no size as it has no type.  so what you're trying to do is,
well, not just impossible, but counterintuitive.
Sep 13 2004
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Jarrett Billingsley wrote:
 if you're trying to do padding in the struct, you can use other types, or
 the align() .. thing.
 
 void itself has no size as it has no type.  so what you're trying to do is,
 well, not just impossible, but counterintuitive.

I got the impression that what he was saying was that, if you used the template parameter 'void', then he wanted the 'optional_member' to be unusable. This doesn't really fit with the C/C++/Java/D paradigm. However, he can do it with a template specialization:
 struct Foo(T) {
   T optional_member;
   int required_member;
 };
  
 struct Foo(T : void) {
   int required_member;
 };
  
 import std.c.stdio;
 void main() {
   alias Foo!(void) Foo1;
   alias Foo!(int)  Foo2;
   Foo1 var1;
   Foo2 var2;
   printf("%d %d\n", var1.sizeof, var2.sizeof);
 }

Sep 13 2004
prev sibling parent Sean Kelly <sean f4.ca> writes:
In article <opseajtwu3aaezs2 ilya.tec.amursk.ru>, Ilya Zaitseff says...
Now following not compiles - "variable optional_member voids have no value"

struct Foo(T)
{
   T optional_member;
   int required_member;
}

void main()
{
   alias Foo!(void) Foo1;
}

Try this: # struct Foo(T) # { # T optional_member; # int required_member; # } # # struct Foo(T:void) # { # int required_member; # } Sean
Sep 13 2004