C
cai
Hello everyone, I am reading Inside the c++ object model(Lippman). In
section 2.3 of Chapter 2, I am puzzled by an example.
class test
{
friend test foo(double);
public:
test()
{memset(array,0,100*sizeof(double));}
private:
double array[100];
}
test foo(double val)
{
test local;
local.array[0]=val;
local.array[99]=val;
return local;
}
main()
{
for(int cnt=0;cnt<1000000;cnt++)
{ test t=foo(double(cnt)); }
return 0;
}
Lippman says that this example couldn't optimize NVR because test class
hasn't a copy constructor. If adding an inline copy constructor like:
inline test::test(const test& t)
{
memcpy(this,&t,sizeof(test));
}
Then NVR is enabled.
But I CAN'T really understand why copy constructor has effect on NVR
optimization, and why it can enable this!!
section 2.3 of Chapter 2, I am puzzled by an example.
class test
{
friend test foo(double);
public:
test()
{memset(array,0,100*sizeof(double));}
private:
double array[100];
}
test foo(double val)
{
test local;
local.array[0]=val;
local.array[99]=val;
return local;
}
main()
{
for(int cnt=0;cnt<1000000;cnt++)
{ test t=foo(double(cnt)); }
return 0;
}
Lippman says that this example couldn't optimize NVR because test class
hasn't a copy constructor. If adding an inline copy constructor like:
inline test::test(const test& t)
{
memcpy(this,&t,sizeof(test));
}
Then NVR is enabled.
But I CAN'T really understand why copy constructor has effect on NVR
optimization, and why it can enable this!!