C89, Mixing Variable Declarations and Code

后端 未结 4 913
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 03:26

I\'m very curious to know why exactly C89 compilers will dump on you when you try to mix variable declarations and code, like this for example:

rutski@imac:~         


        
4条回答
  •  情书的邮戳
    2020-12-17 04:09

    It was probably never implemented that way, because it was never needed.

    Suppose you want to write something like this in plain C:

    int myfunction(int value)
       {
       if (value==0)
          return 0;
       int result = value * 2;
       return result;
       }
    

    Then you can easily rewrite this in valid C, like this:

    int myfunction(int value)
       {
       int result;
       if (value==0)
          return 0;
       result = value * 2;
       return result;
       }
    

    There is absolutely no performance impact by first declaring the variable, then setting its value.

    However, in C++, this is not the case anymore. In the following example, function2 will be slower than function1:

    double function1(const Factory &factory)
       {
       if (!factory.isWorking())
          return 0;
       Product product(factory.makeProduct());
       return product.getQuantity();
       }
    
    double function2(const Factory &factory)
       {
       Product product;
       if (!factory.isWorking())
          return 0;
       product = factory.makeProduct();
       return product.getQuantity();
       }
    

    In function2 the product variable needs to be constructed, even when the factory is not working. Later, the factory makes the product and then the assignment operator needs to copy the product (from the return value of makeProduct to the product variable). In function1, product is only constructed when the factory is working, and even then, the copy constructor is called, not the normal constructor and assignment operator.

    However, I would expect nowadays that a good C++ compiler would optimize this code, but in the first C++ compilers this probably wasn't the case.

    A second example is the following:

    double function1(const Factory &factory)
       {
       if (!factory.isWorking())
          return 0;
       Product &product = factory.getProduct();
       return product.getQuantity();
       }
    
    double function2(const Factory &factory)
       {
       Product &product;
       if (!factory.isWorking())
          return 0;
       product = factory.getProduct();    // Invalid.  You can't assign to a reference.
       return product.getQuantity();
       }
    

    In this example, function2 is simply invalid. References can only be assigned a value at declaration time, not later. This means that in this example, the only way to write valid code is to write the declaration at the moment where the variable is really initialized. Not sooner.

    Both examples show why it was really needed in C++ to allow variable declarations after other executable statements, and not in the beginning of the block like in C. This explains why this was added to C++, and not to C (and other languages) where it isn't really needed.

提交回复
热议问题