Advertisement
Guest User

code sample to highlight differences in compiler behaviour

a guest
Sep 24th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.68 KB | None | 0 0
  1. class A { // no default constructor
  2.     public:
  3.         A(int i) : i_(i) {}
  4.     private:
  5.         int i_ = 0;
  6. };
  7.  
  8. class B { // defines default constructor
  9. public:
  10.         B() {}
  11.         B(int i) : i_(i) {}
  12. private:
  13.         int i_ = 0;
  14. };
  15.  
  16. void foo()
  17. {
  18. // ** - it is the same before and after patch, but behaviour is questionable.
  19. // !  - different before and after patch, but still producess error in the same statement.
  20. // !! - compiler treats statement completely different. E.g. it was compileable before
  21. //      patch and is not compileable after it.
  22. //          after patch -> before patch
  23.  
  24. //
  25. //  Sanity check for non-arrays
  26. //
  27.  
  28.     new A(1);   // ok
  29.     new A();    // error: no matching constructor for initialization of 'A'
  30.     new B(1);   // ok
  31.     new B();    // ok
  32.  
  33. //
  34. //  Simple cases, no default constructor
  35. //
  36.  
  37.     new A[0];           // ** expected ok, not error: no matching constructor for initialization of 'A'
  38.                         // A::A() is NOT expected to be constructed
  39.  
  40.     new A[0] {};        // !! ok -> error: no matching constructor for initialization of 'A'
  41.                         // A::A() is NOT expected to be constructed
  42.  
  43.     new A[0] { {1} };   // ! error: excess elements in array 'new' initializer -> error: no matching constructor for initialization of 'A'
  44.     new A[1];           // error: no matching constructor for initialization of 'A'
  45.     new A[1] {};        // error: no matching constructor for initialization of 'A'
  46.     new A[1] { {1} };   // !! ok -> error: no matching constructor for initialization of 'A'
  47.  
  48.     new A[2] { {1} };           // error: no matching constructor for initialization of 'A'
  49.     new A[2] { {1}, {2} };      // !! ok -> error: no matching constructor for initialization of 'A'
  50.     new A[2] { {1}, {2}, {3} }; // ! error: excess elements in array 'new' initializer -> error: no matching constructor for initialization of 'A'
  51.  
  52. //
  53. //  Simple cases, default constructor is defined
  54. //
  55.  
  56.     new B[0];           // ok
  57.                         // B::B() is NOT expected to be constructed
  58.  
  59.     new B[0] {};        // ok
  60.                         // B::B() is NOT expected to be constructed
  61.  
  62.     new B[0] { {1} };   // !! error: excess elements in array 'new' initializer -> ok
  63.     new B[1];           // ok
  64.     new B[1] {};        // ok
  65.     new B[1] { {1} };   // ok
  66.  
  67.     new B[2] { {1} };           // ok
  68.     new B[2] { {1}, {2} };      // ok
  69.     new B[2] { {1}, {2}, {3} }; // error: excess elements in array 'new' initializer -> ok
  70.  
  71. //
  72. // A bit more complex cases, no default constructor
  73. //
  74.  
  75.     new A[0][0];        // ** expected ok, not error: no matching constructor for initialization of 'A [0]'
  76.                         // A[0] and A::A() are NOT expected to be constructed
  77.  
  78.     new A[0][0] {};     // ok,
  79.                         // A::A() is NOT expected to be constructed
  80.  
  81.     new A[0][1];        // ** expected ok, not error: no matching constructor for initialization of 'A [1]'
  82.                         // A[1] and A::A() are NOT expected to be constructed
  83.  
  84.     new A[1][0];        // ** expected ok, not error: no matching constructor for initialization of 'A [0]'
  85.                         // A[0] and A::A() are NOT expected to be constructed
  86.  
  87.     new A[0][1] {};     // !! ok -> error: no matching constructor for initialization of 'A'
  88.                         // A::A() is NOT expected to be constructed
  89.  
  90.     new A[1][0] {};     // ok
  91.                         // A[0] and A::A() are NOT expected to be constructed
  92.  
  93.     new A[1][1];        // error: no matching constructor for initialization of 'A [1]'
  94.     new A[1][1] {};     // error: no matching constructor for initialization of 'A'
  95.     new A[1][1] { {1} };// !! ok -> error: no matching constructor for initialization of 'A'
  96.  
  97.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  98.         {},             // ! ok -> error: no matching constructor for initialization of 'A'
  99.         {}              // ! ok -> error: no matching constructor for initialization of 'A'
  100.     };
  101.  
  102.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  103.         {},             // ! ok -> error: no matching constructor for initialization of 'A'
  104.         {1}
  105.     };
  106.  
  107.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  108.         {1},
  109.         {}              // ! ok -> error: no matching constructor for initialization of 'A'
  110.     };
  111.  
  112.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  113.         {1},
  114.         {2}
  115.     };                  // ! ok -> error: no matching constructor for initialization of 'A'
  116.  
  117.     new A[1][1] {
  118.         { {1}, {2} }    // error: excess elements in array initializer
  119.     };
  120.  
  121.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  122.         {},             // ! ok -> error: no matching constructor for initialization of 'A'
  123.         { {1} }
  124.     };
  125.  
  126.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  127.         { {1} },
  128.         {}              // ! ok -> error: no matching constructor for initialization of 'A'
  129.     };
  130.  
  131.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  132.         { {1} },
  133.         { {2} }         // ! ok -> error: no matching constructor for initialization of 'A'
  134.     };
  135.  
  136.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  137.        { {1}, {2} },    // ! ok -> excess elements in array initializer
  138.        { {3} }
  139.     };
  140.  
  141.     new A[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  142.         { {1}, {2} },   // ! ok -> excess elements in array initializer
  143.         { {3}, {4} }    // ! ok -> excess elements in array initializer
  144.     };
  145.  
  146.     new A[2][2];        // error: no matching constructor for initialization of 'A[2]'
  147.     new A[2][2] {};     // error: no matching constructor for initialization of 'A'
  148.  
  149.     new A[2][2] {
  150.         {},             // error: no matching constructor for initialization of 'A'
  151.         {}              // error: no matching constructor for initialization of 'A'
  152.     };
  153.  
  154.     new A[2][2] {
  155.         {},             // error: no matching constructor for initialization of 'A'
  156.         { {1} }         // error: no matching constructor for initialization of 'A'
  157.     };
  158.  
  159.     new A[2][2] {
  160.         { {1} },        // error: no matching constructor for initialization of 'A'
  161.         {}              // error: no matching constructor for initialization of 'A'
  162.     };
  163.  
  164.     new A[2][2] {
  165.         { {1} },        // error: no matching constructor for initialization of 'A'
  166.         { {2}, {3} }
  167.     };
  168.  
  169.     new A[2][2] {
  170.         { {1}, {2} },
  171.         { {3} }         // error: no matching constructor for initialization of 'A'
  172.     };
  173.  
  174.     new A[2][2] {
  175.         { {1}, {2} },
  176.         { {3}, {4} }
  177.     };                  // !! ok -> error: no matching constructor for initialization of 'A'
  178.  
  179.     new A[2][2] {
  180.         { {1}, {2}, {3} },  // error: excess elements in array initializer
  181.         { {4} }             // error: no matching constructor for initialization of 'A'
  182.     };
  183.  
  184.     new A[2][2] {
  185.         { {1}, {2}, {3} },  // error: excess elements in array initializer
  186.         { {4}, {5} }
  187.     };
  188.  
  189.     new A[2][2] {
  190.         { {1} },            // error: no matching constructor for initialization of 'A'
  191.         { {2}, {3}, {4} }   // error: excess elements in array initializer
  192.     };
  193.  
  194.     new A[2][2] {
  195.         { {1}, {2} },
  196.         { {3}, {4}, {5} }   // error: excess elements in array initializer
  197.     };
  198.  
  199. //
  200. //  And with default constructor
  201. //
  202.  
  203.     new B[0][0];        // ok
  204.                         // B[0] and B::B() are NOT expected to be constructed
  205.  
  206.     new B[0][0] {};     // ok
  207.                         // B[0] and B::B() are NOT expected to be constructed
  208.  
  209.     new B[0][1];        // ok
  210.                         // B[1] and B::B() is NOT expected to be constructed
  211.  
  212.     new B[1][0];        // ok
  213.                         // B[0] and B::B() are NOT expected to be constructed
  214.  
  215.     new B[0][1] {};     // ok
  216.                         // B[1] and B::B() are NOT expected to be constructed
  217.  
  218.     new B[1][0] {};     // ok
  219.                         // B[0] and B::B() is NOT expected to be constructed
  220.  
  221.     new B[1][1];        // ok
  222.     new B[1][1] {};     // ok
  223.     new B[1][1] { {1} };// ok
  224.  
  225.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  226.         {},
  227.         {}
  228.     };
  229.  
  230.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  231.         {},
  232.         {1}
  233.     };
  234.  
  235.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  236.         {1},
  237.         {}
  238.     };
  239.  
  240.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  241.         {1},
  242.         {2}
  243.     };
  244.  
  245.     new B[1][1] {
  246.         { {1}, {2} }    // error: excess elements in array initializer
  247.     };
  248.  
  249.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  250.         {},
  251.         { {1} }
  252.     };
  253.  
  254.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  255.         { {1} },
  256.         {}
  257.     };
  258.  
  259.     new B[1][1] {       // !! error: excess elements in array 'new' initializer -> ok
  260.         { {1} },
  261.         { {2} }
  262.     };
  263.  
  264.     new B[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  265.        { {1}, {2} },    // ! ok -> error: excess elements in array initializer
  266.        { {3} }
  267.     };
  268.  
  269.     new B[1][1] {       // ! error: excess elements in array 'new' initializer -> ok
  270.         { {1}, {2} },   // ! ok -> error: excess elements in array initializer
  271.         { {3}, {4} }    // ! ok -> error: excess elements in array initializer
  272.     };
  273.  
  274.     new B[2][2];        // ok
  275.     new B[2][2] {};     // ok
  276.  
  277.     new B[2][2] {
  278.         {},
  279.         {}
  280.     };                  // ok
  281.  
  282.     new B[2][2] {
  283.         {},
  284.         { {1} }
  285.     };                  // ok
  286.  
  287.     new B[2][2] {
  288.         { {1} },
  289.         {}
  290.     };                  // ok
  291.  
  292.     new B[2][2] {
  293.         { {1} },
  294.         { {2}, {3} }
  295.     };                  // ok
  296.  
  297.     new B[2][2] {
  298.         { {1}, {2} },
  299.         { {3} }
  300.     };                  // ok
  301.  
  302.     new B[2][2] {
  303.         { {1}, {2} },
  304.         { {3}, {4} }
  305.     };                  // ok
  306.  
  307.     new B[2][2] {
  308.         { {1}, {2}, {3} },  // error: excess elements in array initializer
  309.         { {4} }
  310.     };
  311.  
  312.     new B[2][2] {
  313.         { {1}, {2}, {3} },  // error: excess elements in array initializer
  314.         { {4}, {5} }
  315.     };
  316.  
  317.     new B[2][2] {
  318.         { {1} },
  319.         { {2}, {3}, {4} }   // error: excess elements in array initializer
  320.     };
  321.  
  322.     new B[2][2] {
  323.         { {1}, {2} },
  324.         { {3}, {4}, {5} }   // error: excess elements in array initializer
  325.     };
  326.  
  327. //
  328. //  And a bit of negative stuff
  329. //
  330.  
  331.     new A[-2];          // warning: array size is negative [-Wbad-array-new-length]
  332.                         // ** expected ok, not error: no matching constructor for initialization of 'A'
  333.                         // A::A() is not expected to be constructed, but code for it IS emitted
  334.  
  335.     new A[-2] {};       // warning: array size is negative [-Wbad-array-new-length]
  336.                         // ** expected ok, not error: no matching constructor for initialization of 'A'
  337.                         // A::A() is not expected to be constructed, but code for it IS emitted
  338.  
  339.     new A[-2] { {1} };  // warning: array size is negative [-Wbad-array-new-length]
  340.                         // ! error: excess elements in array 'new' initializer -> error: no matching constructor for initialization of 'A'
  341.  
  342.     new A[-2][0];           // warning: array size is negative [-Wbad-array-new-length]
  343.                             // ** expected ok, not  error: no matching constructor for initialization of 'A [0]'
  344.                             // A[0] and A::A() are not expected to be constructed
  345.  
  346.     new A[-2][0] {};        // warning: array size is negative [-Wbad-array-new-length]
  347.                             // A[0] and A::A() are not expected to be constructed, NOTE: check ll
  348.  
  349.     new A[-2][0] { {1} };   // warning: array size is negative [-Wbad-array-new-length]
  350.                             // ! error: excess elements in array 'new' initializer -> error: excess elements in array initializer
  351.  
  352.     new A[-2][1];           // warning: array size is negative [-Wbad-array-new-length]
  353.                             // ** expected ok, not error: no matching constructor for initialization of 'A [1]'
  354.                             // A[1] and A::A() are not expected to be constructed
  355.  
  356.     new A[-2][1] {};        // warning: array size is negative [-Wbad-array-new-length]
  357.                             // ** expected ok, not error: no matching constructor for initialization of 'A'
  358.                             // A[1] and A::A() are not expected to be constructed
  359.  
  360.     new A[-2][1] { {1} };   // warning: array size is negative [-Wbad-array-new-length]
  361.                             // ! error: excess elements in array 'new' initializer -> error: no matching constructor for initialization of 'A'
  362.  
  363.     new A[-2][-1];                      // error: array size is negative
  364.     new A[-2][-1] {};                   // error: array size is negative
  365.     new A[-2][-1] { {1} };              // error: array size is negative
  366.     new A[-2][-1] { { {1}, {2} } };     // error: array size is negative
  367.     new A[-2][-1] { { {1}, {2}, {3} } };// error: array size is negative
  368.     new B[-2][-1] { {} };               // error: array size is negative
  369.     new B[-2][-1] { { {}, {} } };       // error: array size is negative
  370.     new B[-2][-1] { { {}, {2}, {} } };  // error: array size is negative
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement