value structs : can they have default constructors?

P

Peter Oliphant

I've been told that value structs can have default constructors (I'm using
VS C++.NET 2005 PRO using clr:/pure syntax). But the following code
generates the following error:

value struct ValueStruct
{
double x ;
double y ;
ValueStruct() { x = y = double(0) ; } // error *
} ;

* error C3417: ValueStruct::ValueStruct(void)' : value types cannot contain
user-defined special member functions

Was I misinformed, or am I doing something wrong in the above code?

[==P==]
 
J

jmd.msdn

If you look in "CSharpFinalWorkingDraftApril2005.pdf", you read :

Because every value type implicitly has a public parameterless instance
constructor, it is not possible for a
struct type to contain an explicit declaration of a parameterless
constructor. A struct type is however
permitted to declare parameterized instance constructors (§18.3.8).

jmd
 
J

jmd.msdn

If you also look in "C++-CLI Standard.pdf", you read :

12.2.1 Value classes
A value class is a data structure that contains fields, function members, and nested types. Unlike other class
types, value classes do not support user-defined destructors, finalizers, default constructors, copy
constructors, or copy assignment operators. Value classes are designed to allow the CLI execution engine to
efficiently copy value class objects.

Hope that helps.
jmd
 
P

Peter Oliphant

Because every value type implicitly has a public parameterless instance
constructor, it is not possible for a
struct type to contain an explicit declaration of a parameterless
constructor. A struct type is however
permitted to declare parameterized instance constructors (§18.3.8).

How does the implicit parameterless (i.e., default) constructor determine
the values it gives it members?

Anyway, thanks! This clarifies it for me. I got the exact opposite advice,
or more likely, I misinterpreted the advice (previously) given to me.

So, as I NOW understand it, I can create any number of constructors for a
value struct EXCEPT the default one. That is, I can create constructors
which HAVE parameters. I just tested this, and it does work... : )

[==P==]

jmd.msdn said:
If you look in "CSharpFinalWorkingDraftApril2005.pdf", you read :

Because every value type implicitly has a public parameterless instance
constructor, it is not possible for a
struct type to contain an explicit declaration of a parameterless
constructor. A struct type is however
permitted to declare parameterized instance constructors (§18.3.8).

jmd

Peter Oliphant said:
I've been told that value structs can have default constructors (I'm
using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code
generates the following error:

value struct ValueStruct
{
double x ;
double y ;
ValueStruct() { x = y = double(0) ; } // error *
} ;

* error C3417: ValueStruct::ValueStruct(void)' : value types cannot
contain user-defined special member functions

Was I misinformed, or am I doing something wrong in the above code?

[==P==]
 
H

Holger Grund

Peter Oliphant said:
So, as I NOW understand it, I can create any number of constructors for a
value struct EXCEPT the default one. That is, I can create constructors
which HAVE parameters. I just tested this, and it does work... : )

Value types can't have special member functions. You therefore
also cannot provide a copy constructor for a value type.

-hg
 
P

Peter Oliphant

I guess I then need the definition of 'special member functions'.

Based on what you've said, a copy constructor is a special member function.
But a constructor which, I guess, DOESN'T have as its only parameter a
pointer or reference to the same class type (i.e., isn't a copy constructor)
IS allowed. Why the destinction? Or, like I said, and more importantly what
defines a 'special member function' in contrast to one that isn't 'special'?

[==P==]
 
J

jmd.msdn

Look at http://www.plumhall.com/ecma/index.html
and download the 1.15 C++/CLI draft.

Peter Oliphant said:
I guess I then need the definition of 'special member functions'.

Based on what you've said, a copy constructor is a special member
function. But a constructor which, I guess, DOESN'T have as its only
parameter a pointer or reference to the same class type (i.e., isn't a
copy constructor) IS allowed. Why the destinction? Or, like I said, and
more importantly what defines a 'special member function' in contrast to
one that isn't 'special'?

[==P==]

Holger Grund said:
Value types can't have special member functions. You therefore
also cannot provide a copy constructor for a value type.

-hg
 
T

Tamas Demjen

Peter said:
How does the implicit parameterless (i.e., default) constructor determine
the values it gives it members?

By memset'ing (filling) the entire class with all 0 bytes, regardless of
the actual type of the member variables. Think of value types as PODs
(plain old data, like a C struct). When a value type is copied, the CLR
is simply doing a memcpy on the underlying byte-content (as a raw
octet-stream). If this doesn't satisfy your goals, you must use a ref class.

Tom
 
H

Holger Grund

Peter Oliphant said:
I guess I then need the definition of 'special member functions'.
Special member functions are defined by the C++ standard.
These are:
- default constructor
- copy constructor
- copy assignment operator
- destructor
Based on what you've said, a copy constructor is a special member
function. But a constructor which, I guess, DOESN'T have as its only
parameter a pointer or reference to the same class type (i.e., isn't a
copy constructor) IS allowed. Why the destinction? Or, like I said, and
more importantly what defines a 'special member function' in contrast to
one that isn't 'special'?
The CLR doesn't have concept of copy construction during in ordinary
control flow.

Therefore, there's no way for the C++ compiler to emit information
that other language implementations will understand to implement the
C++ semantics. The language designers apparently figured that this
would cause confusion (and FWIW I disagree) and hence decided
not to allow special member functions. Only special member functions
are called implicitly by the compiler-generated code.

For instance,
// C++
value struct X {
X(){ /*..*/ }
X( const X%){/*..*/}
X% operator=(const X%){ /*...*/};
}

// C# client
void foo() {
X x; // doesn't invoke default ctor
new X[] ( 10 ); // doesn't invoke default ctor for elements
X y = x; // doesn't invoke copy ctor
y = x; // doesn't invoke copy assignment operator
} // doesn't invoke destructor for x or y

-hg
 
P

Peter Oliphant

This is good. That's exactly how I personally think of as the 'natural' way
to default-fill data types one doesn't know anything about. So in this I am
happy! : )

[==P==]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top