Given
value class X
{
public:
// Not allowed: X():i(100000),s (10000) { }
// Allowed
void InitializeDefau lts() { i = 100000; s = 10000; }
private:
int i;
short s;
}
How can:
1)
X x;
x.InitializeDef aults();
be better semantically than
2)
X x;
for setting the default values of 100000 for i and 10000 for s ? In
other words what is the rationale for removing the natural user-defined
default constructor for value types and forcing the user to default
construct the value type object to its zero or null values and then have
to call another function to set default values which the type may want ?
I read that 2) can not be guaranteed to occur properly but 1) evidently
can ? Someone please explain to me how a sequence of two syntax actions
has a better guarantee of properly being implemented than just one. 12 2707 for setting the default values of 100000 for i and 10000 for s ? In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want ?
<from http://www.codecomment s.com/archive292-2006-2-806923.html>
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.
--
Kind regards,
Bruno van Dooren br************* *********@hotma il.com
Remove only "_nos_pam"
Edward Diener wrote: Given
value class X { public: // Not allowed: X():i(100000),s (10000) { } // Allowed void InitializeDefau lts() { i = 100000; s = 10000; } private: int i; short s; }
How can:
1)
X x; x.InitializeDef aults();
be better semantically than
2)
X x;
for setting the default values of 100000 for i and 10000 for s ? In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want ? I read that 2) can not be guaranteed to occur properly but 1) evidently can ? Someone please explain to me how a sequence of two syntax actions has a better guarantee of properly being implemented than just one.
It's simple: The CLR expects to be able to default-construct a value type
by 0-filling it's representation and cannot guarantee that a constructor
will be called.
-cd
That's a strange story. .NET's base spec (the Common Langauge
Infrastructure) does support value types with parameterless constructors,
but only half hearted. Therefore, most languages do not support them.
Unfortunately, C++ managed extensions was an exception.
The most obvious reason why parameterless ctros are not supported is fast
array initialization. Think of this array:
array<V>^ arrayOfVTs = gcnew aray<V>(1000000 );
Assume each VT instance is 8 bytes long. To initialize the array, an
instructroin similar to memset(address of array data, 800000000, 0) is used.
This is possible, because VTs can not have parameterless ctors. With a
parameterless ctor, this would be necessary to initialize the array:
for (int i = 0; i < 1000000; +i)
... call VT .ctor for i-th element ...
This would be significantly slower
Marcus
"Edward Diener" <ed************ *******@tropics oft.com> wrote in message
news:eC******** *****@TK2MSFTNG P03.phx.gbl... Given
value class X { public: // Not allowed: X():i(100000),s (10000) { } // Allowed void InitializeDefau lts() { i = 100000; s = 10000; } private: int i; short s; }
How can:
1)
X x; x.InitializeDef aults();
be better semantically than
2)
X x;
for setting the default values of 100000 for i and 10000 for s ? In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want ? I read that 2) can not be guaranteed to occur properly but 1) evidently can ? Someone please explain to me how a sequence of two syntax actions has a better guarantee of properly being implemented than just one.
Marcus Heege wrote: That's a strange story. .NET's base spec (the Common Langauge Infrastructure) does support value types with parameterless constructors, but only half hearted. Therefore, most languages do not support them. Unfortunately, C++ managed extensions was an exception.
The most obvious reason why parameterless ctros are not supported is fast array initialization. Think of this array:
array<V>^ arrayOfVTs = gcnew aray<V>(1000000 );
Assume each VT instance is 8 bytes long. To initialize the array, an instructroin similar to memset(address of array data, 800000000, 0) is used. This is possible, because VTs can not have parameterless ctors. With a parameterless ctor, this would be necessary to initialize the array:
for (int i = 0; i < 1000000; +i) ... call VT .ctor for i-th element ...
This would be significantly slower
Good, it should be slower. If the programmer wants to provide a
user-defined default constructor for a type, then filling an array
should construct each value of that type with that default constructor.
That is the normal expectation. Skewing the language/implementation is
not the way to go.
In native C++ if I create a std::vector with a size of 1000000 then each
element of my vector will be default constructed. I see no reason why
..NET should not have followed this same rule instead of choosing the bad
idea that default constructors will not be allowed for value types.
In real life of course nobody creates an array of 1 million elements.
I am piqued about this because I know that MS made a bad decision in not
allowing normal constructors/destructors for value types. But it is too
late to change anything. Once the wrong path is taken nobody will admit
to failure. Marcus
"Edward Diener" <ed************ *******@tropics oft.com> wrote in message news:eC******** *****@TK2MSFTNG P03.phx.gbl... Given
value class X { public: // Not allowed: X():i(100000),s (10000) { } // Allowed void InitializeDefau lts() { i = 100000; s = 10000; } private: int i; short s; }
How can:
1)
X x; x.InitializeDef aults();
be better semantically than
2)
X x;
for setting the default values of 100000 for i and 10000 for s ? In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want ? I read that 2) can not be guaranteed to occur properly but 1) evidently can ? Someone please explain to me how a sequence of two syntax actions has a better guarantee of properly being implemented than just one.
Carl Daniel [VC++ MVP] wrote: Edward Diener wrote: Given
value class X { public: // Not allowed: X():i(100000),s (10000) { } // Allowed void InitializeDefau lts() { i = 100000; s = 10000; } private: int i; short s; }
How can:
1)
X x; x.InitializeDef aults();
be better semantically than
2)
X x;
for setting the default values of 100000 for i and 10000 for s ? In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want ? I read that 2) can not be guaranteed to occur properly but 1) evidently can ? Someone please explain to me how a sequence of two syntax actions has a better guarantee of properly being implemented than just one.
It's simple: The CLR expects to be able to default-construct a value type by 0-filling it's representation and cannot guarantee that a constructor will be called.
I clearly did not ask how things worked but why. Your answer to my
question is "that's the way things are."
The array argument I am constantly given is flawed. A native C++
std::vector<X> of n elements default constructs each element. CLRs
attempts to get around this are just plain wrong and create a ridiculous
anomaly which tells me that I can create constructors for value types
but I can not create a default constructor for a value type. What a bad
design decision that is. OK, what's the difference. Like everybody else
I will have to kludge my way around this abortion. I just expected
better from MS.
Bruno van Dooren wrote: for setting the default values of 100000 for i and 10000 for s ? In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want ? <from http://www.codecomment s.com/archive292-2006-2-806923.html>
Please, I know the rule. I clearly asked why and not how. 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.
Edward Diener wrote: Carl Daniel [VC++ MVP] wrote: It's simple: The CLR expects to be able to default-construct a value type by 0-filling it's representation and cannot guarantee that a constructor will be called.
I clearly did not ask how things worked but why. Your answer to my question is "that's the way things are."
Oh contraire - you asked why C++/CLI prohibits defining a copy constructor -
and the reason I gave is correct: the CLR doesn't support it.
Why the CLR makes such a requirement is another question - and one that I've
never seen a satisfactory answer for. I can't help but wonder if it's
simply due to lack of planning at some very deep level in the CLR. Having
value types designed such that it's not necessary to run a default
constructor makes sense. Having them defined so it's not possible to
(always) run a default constructor seems like a design omission to me.
-cd
Carl Daniel [VC++ MVP] wrote: Edward Diener wrote: Carl Daniel [VC++ MVP] wrote: It's simple: The CLR expects to be able to default-construct a value type by 0-filling it's representation and cannot guarantee that a constructor will be called. I clearly did not ask how things worked but why. Your answer to my question is "that's the way things are."
Oh contraire - you asked why C++/CLI prohibits defining a copy constructor -
No, a default constructor.
and the reason I gave is correct: the CLR doesn't support it.
Why the CLR makes such a requirement is another question - and one that I've never seen a satisfactory answer for. I can't help but wonder if it's simply due to lack of planning at some very deep level in the CLR. Having value types designed such that it's not necessary to run a default constructor
Now you have it right.
makes sense. Having them defined so it's not possible to (always) run a default constructor seems like a design omission to me.
I am glad someone agrees with me here. Having to write a kludge, either
a dummy constructor which takes at least one argument but actual does
default construction, or a separate function to be called to do default
construction, seems really silly to me. I know I can use a ref class
instead but I see nothing wrong with simple value classes which
initialize their variables to something other than 0 when default
constructed.
"Edward Diener" <ed************ *******@tropics oft.com> skrev i
meddelandet news:u8******** ******@TK2MSFTN GP03.phx.gbl... Carl Daniel [VC++ MVP] wrote: It's simple: The CLR expects to be able to default-construct a value type by 0-filling it's representation and cannot guarantee that a constructor will be called.
I clearly did not ask how things worked but why. Your answer to my question is "that's the way things are."
The CLR wasn't designed for C++, but for primarilly for C#.
Now, when adopting C++/CLI to the existing infrastructure, we just
have to accept it "the way things are". C++ is still not a first class
citizen of .NET. C++/CLI is, and it turned out to be different.
Bo Persson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matt Feinstein |
last post by:
Hi all--
I'm new to Python, and was somewhat taken aback to discover that the
core language lacks some basic numerical types (e.g., single-precision
float, short integers). I realize that there are extensions that add
these types-- But what's the rationale for leaving them out? Have I
wandered into a zone in the space/time continuum where people never
have to read binary data files?
Matt Feinstein
|
by: sneill |
last post by:
How is it possible to take the value of a variable (in this case,
MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name?
In the following example I want 'oIcon' object to have the properties:
mode1, mode2, and mode3.
This seems simple but I can't quite figure it out...
Any ideas anyone?
|
by: Andreas Huber |
last post by:
Hi there
Spending half an hour searching through the archive I haven't found a
rationale for the following behavior.
using System;
// note the missing Flags attribute
enum Color
{
|
by: Jose Fernandez |
last post by:
Hello.
I'm building a web service and I get this error.
NEWS.News.CoverNews(string)': not all code paths return a value
This is the WebMethod
public SqlDataReader CoverNews(string Sport)
{
|
by: Ben R. |
last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing
with value versus object types. If I'm correct, when I do:
dim x as integer
There's no need for "new" to be called since this "value" type is all set to
go. Is this because value types are stored on the stack so the memory
initialization is already taken care of?
Now, when dealing with an object type, it seems you do need to use the "new"
| |
by: utab |
last post by:
Dear all,
Can somebody direct me to some resources on the subject or explain the
details in brief? I checked the FAQ but could not find or maybe missed.
Regards,
|
by: Jess |
last post by:
Hello,
I understand the default-initialization happens if we don't initialize
an object explicitly. I think for an object of a class type, the
value is determined by the constructor, and for the built-in types,
the value is usually garbage. Is this right?
However, I'm a bit confused about value-initialization, when does it
happen, and what kind of values are assigned to objects?
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially...
|
by: Macneed |
last post by:
i am a newbie,
i remember i read a book talking about when u declare a array variable
using
float ABC = new float;
the whole array element in ABC ( ABC to ABC ) will automatic
initialize to 0
and all type of numeric variable is initialize to 0, bool type is false,
is it true?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |