Date: Sun, 30 Apr 2023 12:24:16 +0100
Before the compiler produces code to create an object, it knows how
many constructors will be called. The count of 'how many before' and
'how many after' could be made available to the programmer. Consider
the following code:
#include <iostream>
struct LifeForm {
LifeForm(void)
{
std::cout << "Lifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct CarbonBasedLifeForm : virtual LifeForm {
CarbonBasedLifeForm(void)
{
std::cout << "CarbonBaseLifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct OxygenFuelledLifeForm : virtual LifeForm {
OxygenFuelledLifeForm(void)
{
std::cout << "OxygenFuelledLifeForm -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct Mammal : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Mammal(void)
{
std::cout << "Mammal -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct Fish : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Fish(void)
{
std::cout << "Fish -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct FreakHybrid : Mammal, Fish {
FreakHybrid(void)
{
std::cout << "FreakHybrid -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
int main(void)
{
FreakHybrid m;
}
The above code would print out the following:
Lifeform -- Before: 0, After: 5
OxygenFuelledLifeForm -- Before: 1, After: 4
CarbonBaseLifeform -- Before: 2, After: 3
Mammal -- Before: 3, After: 2
Fish -- Before: 4, After: 1
FreakHybrid -- Before: 5, After: 0
as though it had been written as follows:
https://godbolt.org/z/8Y4z8x1EE
and copy-pasted:
#include <iostream>
namespace std {
thread_local unsigned how_many_constructors_before,
how_many_constructors_after;
}
struct LifeForm {
LifeForm(void)
{
std::cout << "Lifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct CarbonBasedLifeForm : virtual LifeForm {
CarbonBasedLifeForm(void)
{
std::cout << "CarbonBaseLifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct OxygenFuelledLifeForm : virtual LifeForm {
OxygenFuelledLifeForm(void)
{
std::cout << "OxygenFuelledLifeForm -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct Mammal : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Mammal(void)
{
std::cout << "Mammal -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct Fish : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Fish(void)
{
std::cout << "Fish -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct FreakHybrid : Mammal, Fish {
FreakHybrid(void)
{
std::cout << "FreakHybrid -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
int main(void)
{
std::how_many_constructors_before = 0u;
std::how_many_constructors_after = 5u;
FreakHybrid m;
}
many constructors will be called. The count of 'how many before' and
'how many after' could be made available to the programmer. Consider
the following code:
#include <iostream>
struct LifeForm {
LifeForm(void)
{
std::cout << "Lifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct CarbonBasedLifeForm : virtual LifeForm {
CarbonBasedLifeForm(void)
{
std::cout << "CarbonBaseLifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct OxygenFuelledLifeForm : virtual LifeForm {
OxygenFuelledLifeForm(void)
{
std::cout << "OxygenFuelledLifeForm -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct Mammal : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Mammal(void)
{
std::cout << "Mammal -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct Fish : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Fish(void)
{
std::cout << "Fish -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
struct FreakHybrid : Mammal, Fish {
FreakHybrid(void)
{
std::cout << "FreakHybrid -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
}
};
int main(void)
{
FreakHybrid m;
}
The above code would print out the following:
Lifeform -- Before: 0, After: 5
OxygenFuelledLifeForm -- Before: 1, After: 4
CarbonBaseLifeform -- Before: 2, After: 3
Mammal -- Before: 3, After: 2
Fish -- Before: 4, After: 1
FreakHybrid -- Before: 5, After: 0
as though it had been written as follows:
https://godbolt.org/z/8Y4z8x1EE
and copy-pasted:
#include <iostream>
namespace std {
thread_local unsigned how_many_constructors_before,
how_many_constructors_after;
}
struct LifeForm {
LifeForm(void)
{
std::cout << "Lifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct CarbonBasedLifeForm : virtual LifeForm {
CarbonBasedLifeForm(void)
{
std::cout << "CarbonBaseLifeform -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct OxygenFuelledLifeForm : virtual LifeForm {
OxygenFuelledLifeForm(void)
{
std::cout << "OxygenFuelledLifeForm -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct Mammal : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Mammal(void)
{
std::cout << "Mammal -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct Fish : virtual OxygenFuelledLifeForm, virtual CarbonBasedLifeForm {
Fish(void)
{
std::cout << "Fish -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
struct FreakHybrid : Mammal, Fish {
FreakHybrid(void)
{
std::cout << "FreakHybrid -- Before: " <<
std::how_many_constructors_before << ", After: " <<
std::how_many_constructors_after << std::endl;
++std::how_many_constructors_before;
--std::how_many_constructors_after ;
}
};
int main(void)
{
std::how_many_constructors_before = 0u;
std::how_many_constructors_after = 5u;
FreakHybrid m;
}
Received on 2023-04-30 11:24:25