C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::how_many_constructors_after

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Sun, 30 Apr 2023 14:22:44 +0200
The counters would need to be stored inside the objects, as during the construction other objects can be constructed.   Would this be compatible with multiple, private and virtual inheritance? Where constructors for the same sub-object type is called several times at different kernels in the hierarchy.   Why would you want to get this information?   Why should those variables be automatically generated? That makes it more difficult to understand the size and member variables within an object. Not very C++ like to increase object size without explicit control.     -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:So 30.04.2023 13:24 Betreff:[std-proposals] std::how_many_constructors_after An:std-proposals <std-proposals_at_[hidden]>; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; 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; } -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-04-30 12:22:47