Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Code Review

Code Review

##Code Review

Code Review

Fixed typos
Source Link
Toby Speight
  • 87.1k
  • 14
  • 104
  • 322

You should probably note that this code is now also available under the creative common'sCreative Commons license which you agreed to by posting it on this page. See the base of the page for details.

In the old days this was not lelgallegal (the space after the hash). But I am not sure if this has been updated.

Also I am concerned about the return type. Why are you returning a reference to the internal integer? Should that not be a reference to the safe_intsafe_int? Is this some kind of pre-mature optimization?

It has to be an intint so passing by value seems OK until you come along and adapt the class for some other type (might be useful for std::complexstd::complex<int>?). Then passing by value is no longer a good solution.

Are you sure thatsthat's a a dafesafe test?

What about signed integer?
Do all systems use integers where the std::abs(min) > max std::abs(min) > max? Not sure I know that answer.

You could think of no other way of writing that without the gotogoto?

You should probably note that this code is now also available under the creative common's license which you agreed to by posting it on this page. See the base of the page for details.

In the old days this was not lelgal (the space after the hash). But I am not sure if this has been updated.

Also I am concerned about the return type. Why are you returning a reference to the internal integer? Should that not be a reference to the safe_int? Is this some kind of pre-mature optimization?

It has to be an int so passing by value seems OK until you come along and adapt the class for some other type (might be useful for std::complex?). Then passing by value is no longer a good solution.

Are you sure thats a a dafe test?

What about signed integer?
Do all systems use integers where the std::abs(min) > max ? Not sure I know that answer.

You could think of no other way of writing that without the goto?

You should probably note that this code is now also available under the Creative Commons license which you agreed to by posting it on this page. See the base of the page for details.

In the old days this was not legal (the space after the hash). But I am not sure if this has been updated.

Also I am concerned about the return type. Why are you returning a reference to the internal integer? Should that not be a reference to the safe_int? Is this some kind of pre-mature optimization?

It has to be an int so passing by value seems OK until you come along and adapt the class for some other type (might be useful for std::complex<int>?). Then passing by value is no longer a good solution.

Are you sure that's a a safe test?

What about signed integer?
Do all systems use integers where the std::abs(min) > max? Not sure I know that answer.

You could think of no other way of writing that without the goto?

Source Link
Loki Astari
  • 97.6k
  • 5
  • 126
  • 341

##Code Review

/*
 * Copyright © 2020 James Larrowe
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

You should probably note that this code is now also available under the creative common's license which you agreed to by posting it on this page. See the base of the page for details.

user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020年1月30日.35920


In the old days this was not lelgal (the space after the hash). But I am not sure if this has been updated.

# define SAFE_INTEGER_HPP 1

Anyway it looks untidy :-)


Here you have undefined behavior.

 I &operator=(I v) { val = v; }

You are supposed to return a reference yet don't return anything.

Also I am concerned about the return type. Why are you returning a reference to the internal integer? Should that not be a reference to the safe_int? Is this some kind of pre-mature optimization?

It has to be an int so passing by value seems OK until you come along and adapt the class for some other type (might be useful for std::complex?). Then passing by value is no longer a good solution.

Assume somebody will use your class in a way you have not anticipated and thus program defensively. Pass parameters by const reference unless you know that forever it is only going to be an int and you can't know that because its an I.


Are you sure thats a a dafe test?

 safe_int operator-()
 {
 if(val < -max)
 throw std::overflow_error("");
 return safe_int(-val);
 }

What about signed integer?
Do all systems use integers where the std::abs(min) > max ? Not sure I know that answer.


Hard to spot that &.

 safe_int &operator++()

In C++ (unlike C) the & and the * are usually placed with the type information. So it would normally be written as:

 safe_int& operator++()

You could think of no other way of writing that without the goto?

 safe_int &operator*=(I rhs)
 {
 goto no_overflow;
no_overflow:
 val *= rhs;
 return *this;
 }
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /