Skip to main content
Mod Moved Comments To Chat
Improved wording
Source Link
Rakete1111
  • 2.6k
  • 1
  • 16
  • 23
  1. That private is unnecessary, the default access specifier for a class is already private:

     class number_bag {
     /*private*/
         size_t        m_size;
         FloatingPoint m_sum;
         FloatingPoint m_square_sum;
    
  2. Mark functions that doshould not throw noexcept (in your case, all of them). This helps the compiler optimize if you compile with exceptions.

  3. Use std::move to avoid unnecessary copies:

     number_bag(size_t size, FloatingPoint sum, FloatingPoint square_sum) :
         m_size{size},
         m_sum{std::move(sum)},
         m_square_sum{std::move(square_sum)} {}
    
  1. That private is unnecessary, the default access specifier for a class is already private:

     class number_bag {
     /*private*/
         size_t        m_size;
         FloatingPoint m_sum;
         FloatingPoint m_square_sum;
    
  2. Mark functions that do not throw noexcept (in your case, all of them). This helps the compiler optimize if you compile with exceptions.

  3. Use std::move to avoid unnecessary copies:

     number_bag(size_t size, FloatingPoint sum, FloatingPoint square_sum) :
         m_size{size},
         m_sum{std::move(sum)},
         m_square_sum{std::move(square_sum)} {}
    
  1. That private is unnecessary, the default access specifier for a class is already private:

     class number_bag {
     /*private*/
         size_t        m_size;
         FloatingPoint m_sum;
         FloatingPoint m_square_sum;
    
  2. Mark functions that should not throw noexcept. This helps the compiler optimize if you compile with exceptions.

  3. Use std::move to avoid unnecessary copies:

     number_bag(size_t size, FloatingPoint sum, FloatingPoint square_sum) :
         m_size{size},
         m_sum{std::move(sum)},
         m_square_sum{std::move(square_sum)} {}
    
added 60 characters in body
Source Link
Rakete1111
  • 2.6k
  • 1
  • 16
  • 23
  1. CompileIf you compile with gcc, use -Wall -Wextra -pedantic (or every warning turned on, with extensions disabled) - As posted, your code generates 2 warnings:

     main.cpp: In function 'int main(int, const char**)':
     main.cpp:113:14: warning: unused parameter 'argc' [-Wunused-parameter]
      int main(int argc, const char * argv[]) {
           ^~~~
     main.cpp:113:38: warning: unused parameter 'argv' [-Wunused-parameter]
      int main(int argc, const char * argv[]) {
                                   ^
    
  1. Compile with -Wall (or every warning turned on) - As posted, your code generates 2 warnings:

     main.cpp: In function 'int main(int, const char**)':
     main.cpp:113:14: warning: unused parameter 'argc' [-Wunused-parameter]
      int main(int argc, const char * argv[]) {
           ^~~~
     main.cpp:113:38: warning: unused parameter 'argv' [-Wunused-parameter]
      int main(int argc, const char * argv[]) {
                                   ^
    
  1. If you compile with gcc, use -Wall -Wextra -pedantic (or every warning turned on, with extensions disabled) - As posted, your code generates 2 warnings:

     main.cpp: In function 'int main(int, const char**)':
     main.cpp:113:14: warning: unused parameter 'argc' [-Wunused-parameter]
      int main(int argc, const char * argv[]) {
           ^~~~
     main.cpp:113:38: warning: unused parameter 'argv' [-Wunused-parameter]
      int main(int argc, const char * argv[]) {
                                   ^
    
added 277 characters in body
Source Link
Rakete1111
  • 2.6k
  • 1
  • 16
  • 23
  1. Even if fp_type was necessary, please don't define it to just use it once, that's not really efficient in terms of code size.

  2. Use auto to simplify operator+'s and operator-'s return type.

  3. Don't use C-style casts, use C++ style casts instead. In your case, you can even use long double literals instead of an ugly cast:

    auto bag3 = bag2 + 1.0l; // or 1.l
    
  4. Always use the pre-increment operator (++i;) instead of the post-increment operator (i++;), unless you have a specific reason not to. Although a good compiler might generate in almost every case the same assembly code, there can exist a class that has side effects in its construction, and as such a compiler is not allowed to remove those.

  5. this-> is in every case you use it redundant. Remove it, it just clutters the view. :)

  6. I don't see why you implemented your own copy constructor and copy assignment operator. You literally have no reason to, they do the same as the implicit defined ones.

  1. Even if fp_type was necessary, please don't define it to just use it once, that's not really efficient in terms of code size.

  2. Use auto to simplify operator+'s and operator-'s return type.

  3. Don't use C-style casts, use C++ style casts instead. In your case, you can even use long double literals instead of an ugly cast:

    auto bag3 = bag2 + 1.0l; // or 1.l
    
  4. Always use pre-increment, unless you have a specific reason not to.

  5. this-> is in every case you use it redundant. Remove it, it just clutters the view. :)

  6. I don't see why you implemented your own copy constructor and copy assignment operator. You literally have no reason to, they do the same as the implicit defined ones.

  1. Even if fp_type was necessary, please don't define it to just use it once, that's not really efficient in terms of code size.

  2. Use auto to simplify operator+'s and operator-'s return type.

  3. Don't use C-style casts, use C++ style casts instead. In your case, you can even use long double literals instead of an ugly cast:

    auto bag3 = bag2 + 1.0l; // or 1.l
    
  4. Always use the pre-increment operator (++i;) instead of the post-increment operator (i++;), unless you have a specific reason not to. Although a good compiler might generate in almost every case the same assembly code, there can exist a class that has side effects in its construction, and as such a compiler is not allowed to remove those.

  5. this-> is in every case you use it redundant. Remove it, it just clutters the view. :)

  6. I don't see why you implemented your own copy constructor and copy assignment operator. You literally have no reason to, they do the same as the implicit defined ones.

[@darhuuk](https://codereview.stackexchange.com/users/117457/darhuuk) made me realize that `std::common_type` is the wrong tool for the job.
Source Link
Rakete1111
  • 2.6k
  • 1
  • 16
  • 23
Loading
added 339 characters in body
Source Link
Rakete1111
  • 2.6k
  • 1
  • 16
  • 23
Loading
Source Link
Rakete1111
  • 2.6k
  • 1
  • 16
  • 23
Loading