26

Re: Constant copy of a class. Who as does?

MasterZiv, made easier:

template <class T = std:: nullptr_t> class B;
template <>
class B <std:: nullptr_t>: public A
{
public:
B ()
{
printf ("B:: B () \r\n");
}
virtual ~B ()
{
printf ("B:: ~B () \r\n");
}
};
template <>
class B <const>: public A
{
public:
B ()
{
printf ("const B:: B () \r\n");
}
virtual ~B ()
{
printf ("const B:: ~B () \r\n");
}
};

the Declaration:

C <> c1;
C <const> c2;

It is a pity in a C ++ there is no the reserved keyword allowing in default to substitute <cv-qualifiers type> from the declaration lvalue;
The moderator: here a shit Will rush, I will cut both from a forum. Not only you concerns.
it is led.

27

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

Generally, in a C ++ it is a lot of "innuendo". For example:
* There is a possibility to "tell" new const Type (), but there is no possibility this const though somehow to "hear" in implementation of the overloaded operator new, the designer or  a class;
* There is a possibility to redefine placement new, but the compiler does not feign its call at determination of a copy of object on a stack though, such behavior would be logical;
* We can  templates with deduction on parameters of the designer, but we can not  templates with deduction on type and declaration qualifiers lvalue;

Simply at you in a head any other language, and you for some reason it name "With ++"...

28

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

blonduser, you will not wait! smile))
With deduction it is impossible, and without it quits banal exactly specialized template which even the schoolboy writes.

Well you nevertheless strongly overestimate schoolboys.
In mass they at all do not know that such specialization.

29

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

it is passed...
Simply at you in a head any other language, and you for some reason it name "With ++"...

You mean only the last remark or all three?

30

Re: Constant copy of a class. Who as does?

It was very interesting to me to see, how and when you will release those objects which did not delete in  when the class has been declared as "const"?

31

Re: Constant copy of a class. Who as does?

blonduser wrote:

it was very interesting To me to see, how and when you will release those objects which did not delete in  when the class has been declared as "const"?

What for to release what is not selected?

32

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

What for to release what is not selected?

It followed from your first post and from.
Normally do the designer with parameters and already depending on them or create member-object, or to the pointer appropriate NULL .
In  clear those objects which pointer not NULL and there is no necessity to create additional keys and to invent templates.

33

Re: Constant copy of a class. Who as does?

blonduser wrote:

In  clear those objects which pointer not NULL and there is no necessity to create additional keys and to invent templates.

it is good, I will explain on fingers:
Let's admit, in the designer the pointer for a constant line from static resources of the binary unit and if not constant, this line it is necessary to copy object in a heap is transferred and to initialize a term-index for the copied line, and if constant it is possible to initialize simply the pointer this constant line without its copying. Accordingly, at corrupting of not constant object you should release storage from towards the lines, and at corrupting of constant object of it it is not necessary to do. So it is more clear?

34

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

it is passed...
Well, I will explain on fingers:
Let's admit, in the designer the pointer for a constant line from static resources of the binary unit and if not constant, this line it is necessary to copy object in a heap is transferred and to initialize a term-index for the copied line, and if constant it is possible to initialize simply the pointer this constant line without its copying. Accordingly, at corrupting of not constant object you should release storage from towards the lines, and at corrupting of constant object of it it is not necessary to do. So it is more clear?

[spoiler]

//Example program
#include <iostream>
#include <string>
#include <cstring>
class const_C {
const char *const_string;
char *nonconst_string;
public:
const_C (const char* s) {const_string = s; nonconst_string = NULL;}
const_C (char* s) {const_string = NULL; nonconst_string = strdup (s);}
void print_state () {if (const_string) std:: cout <<"i'm a const inited\n"; else if (nonconst_string) std:: cout <<"i must free a string\n";}
};
const char cstr [] = "hello const";
int main ()
{
char* str = "non const";
const_C c_obj (cstr);
const_C obj (str);
c_obj.print_state ();
obj.print_state ();
}

[/spoiler]
http://cpp.sh/5aj3m
The designer clearly how to add?

35

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

it is good, I will explain on fingers:
Let's admit, in the designer the pointer for a constant line from static resources of the binary unit and if not constant, this line it is necessary to copy object in a heap is transferred and to initialize a term-index for the copied line, and if constant it is possible to initialize simply the pointer this constant line without its copying. Accordingly, at corrupting of not constant object you should release storage from towards the lines, and at corrupting of constant object of it it is not necessary to do. So it is more clear?

a magnificent mine, with the excellent, postponed fuse)) all here is fine)))

36

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

it is admissible, in the designer the pointer for a constant line from static resources of the binary unit and if not constant, this line it is necessary to copy object in a heap is transferred and to initialize a term-index for the copied line, and if constant it is possible to initialize simply the pointer this constant line without its copying. Accordingly, at corrupting of not constant object you should release storage from towards the lines, and at corrupting of constant object of it it is not necessary to do.

But you understand, what the pointer can be made not constant in a call and to change a line somehow?
For a line I saw a template in which the mechanism which you is implemented described.
But there objects of a line, and the pointer for the line one are transferred.
If at some instant time the line has to be changed, there is a copy of a line.
At removal of the last object the line is cleared also.
Here such mechanism does not approach you?

37

Re: Constant copy of a class. Who as does?

blonduser wrote:

But you understand, what the pointer can be made not constant in a call and to change a line somehow?

If a member-index private it is implementation only mine. For example:

struct CTest
{
private:
char* pStr;
public:
CTest (const char *);
CTest& operator = (CTest&&);
CTest& operator + = (CTest&&);
};
blonduser wrote:

for a line I saw a template in which the mechanism which you is implemented described.
But there objects of a line, and the pointer for the line one are transferred.
If at some instant time the line has to be changed, there is a copy of a line.
At removal of the last object the line is cleared also.

I already thought over it but as at me in structure cannot is stored anything, except only one pointer, it is necessary to make static a variable in any protected a method, specifying in a copy std:: map which will be added from the designer (to be cleaned from ) and to store pointers on copies of structure with initialization flags, and the method will return an initialization state.
But at any implementation there is one hitch - by call CTest ("The test message"), the short line can be transferred entirely in a stack frame together with the pointer to it and, naturally, can be jammed frames of other calls after an output from visibility area. Or, the pointer for the line from a heap can be transferred, and then storage from under this line will be released.
As a result it turns out that at usage of not so big lines, an overhead charge for catching of all these nuances superimpose advantages from  lines.

38

Re: Constant copy of a class. Who as does?

egorych wrote:

a magnificent mine, with the excellent, postponed fuse)) all here is fine)))

And that!

39

Re: Constant copy of a class. Who as does?

Siemargl, the pointer in structure, in essence, only one.

40

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

I already thought over it but as at me in structure cannot is stored anything, except only one pointer

Well so it is enough one pointer.
It can appropriate anything you like the main thing not to forget that appropriated.

41

Re: Constant copy of a class. Who as does?

If to space apart  the designer and initialization basically it is possible.

class A C
{
char* data;
public:
C ()
{
//empty constructor, nothing to do in destructor
data = NULL;
};
void init (const char* src)
{
//non-const constructor replacer
printf ("non-const %s\n", src);
};
void init (const char* src) const
{
//const constructor replacer
printf ("const %s\n", src);
};
~C ()
{
if (data)
{
//destroy owned data
}
};
};

42

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

Siemargl, the pointer in structure, in essence, only one.

Then it is necessary to store a flag  initialization.
But generally that to do a copy anyway, if only the data not transcendental kol-in easier.

43

Re: Constant copy of a class. Who as does?

Still it is possible to use constexpr and to carry out object lifetime generally on a compilation stage.

44

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

it is passed...
You mean only the last remark or all three?

All three.

45

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

it is passed...
Well, I will explain on fingers:
Let's admit, in the designer the pointer for a constant line from static resources of the binary unit and if not constant, this line it is necessary to copy object in a heap is transferred and to initialize a term-index for the copied line, and if constant it is possible to initialize simply the pointer this constant line without its copying. Accordingly, at corrupting of not constant object you should release storage from towards the lines, and at corrupting of constant object of it it is not necessary to do. So it is more clear?

Your representations about an object model With ++ are slightly easier, than it is actually.
Besides constant and not constant objects there are still objects temporal, and semantics of relocation which works both with constant, and with not constant, and with transient objects.
It is necessary to you SEPARATELY, ORTHOGONALLY To  OBJECT to implement this feature:
Your object should store either the link to a constant or real value, and the nobility, on what he refers at present.
If the object refers for a constant line from . Resources to work with it while the object does not change in this aspect.
If this line changes in the given object to change it for the link to constant resources or to real value.
At removal to release something only if we store real value.
And to make it, apparently, it is simple enough, it is necessary to store inside
std:: variant <std:: string, std:: string_view>
I do not know all your semantics but it seems that it approaches you, thus it is not necessary to do almost generally anything.

46

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

All three.

Give on points?
* There is a possibility to "tell" new const Type (), but there is no possibility this const though somehow to "hear" in implementation of the overloaded operator new, the designer or  a class;
What for possibility to write const if at creation of a copy of a class in a heap this const, the equal account, on what does not influence?
* There is a possibility to redefine placement new, but the compiler does not feign its call at determination of a copy of object on a stack though, such behavior would be logical;
At layout of a copy of a class on a stack the compiler itself selects storage on a stack and initializes there a class copy that is approximately equivalent:

MyClass* p = (MyClass *) alloca (sizeof (MyClass));
if (NULL! = p) new (p) MyClass ();

So why to the compiler not to create placement new by default if it is not redefined and not to feign its call? Would receive more controlled behavior.
* we Can  templates with deduction on parameters of the designer, but we can not  templates with deduction on type and declaration qualifiers lvalue;
Is using which are used similarly typedef! So why not to provide in them usage of cv-qualifiers c the help of any suitable semantics? Something of type:

using MyClassWrapper = MyClass <>;
using const MyClassWrapper = const MyClass <const>;

47

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

Your representations about an object model With ++ are slightly easier, than it is actually.
Besides constant and not constant objects there are still objects temporal, and semantics of relocation which works both with constant, and with not constant, and with transient objects.
It is necessary to you SEPARATELY, ORTHOGONALLY To  OBJECT to implement this feature:
Your object should store either the link to a constant or real value, and the nobility, on what he refers at present.
If the object refers for a constant line from . Resources to work with it while the object does not change in this aspect.
If this line changes in the given object to change it for the link to constant resources or to real value.
At removal to release something only if we store real value.
And to make it, apparently, it is simple enough, it is necessary to store inside
std:: variant <std:: string, std:: string_view>
I do not know all your semantics but it seems that it approaches you, thus it is not necessary to do almost generally anything.

I, meanwhile, gave up thought to implement this crazy idea right now, but for myself decided to dance from reverse - if constant value is in storage of the unit of process and the storage selected under it read-only to initialize without copying and to store this sign in std:: map in which will specify the static pointer inside protected the virtual method. Easier now to study laziness MSDN about all these dancings round obtaining of the information on pages of storage of the unit as now there is no emergency of operation with the big lines or .

48

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

At layout of a copy of a class on a stack the compiler itself selects storage on a stack and initializes there a class copy that approximately is equivalent :

MyClass* p = (MyClass *) alloca (sizeof (MyClass));
if (NULL! = p) new (p) MyClass ();

Thanks,  smile))

49

Re: Constant copy of a class. Who as does?

NekZ wrote:

Thanks,  smile))

On , still ! smile

50

Re: Constant copy of a class. Who as does?

NekZ wrote:

it is passed...
Thanks,  smile))

Or you to me want to tell, how the compiler interposes the machine code operating with registers (R/E) SP and (R/E) BP? It is not necessary!... Exactly too most function _malloca does.