C++ 通过函数中的引用操纵值的安全性

C++ 通过函数中的引用操纵值的安全性,c++,reference,return,C++,Reference,Return,关于风格的简单问题。这两种方法都有效(如下所述),我想知道哪种被认为是更好/更安全的做法? 我为添加提供了整数溢出保护,并为删除定义了一些功能。两种方法返回相同的结果 #include <iostream> #include <limits> void safe_add(int&, int); void safe_remove(int&, int); int main() { int x = 50; safe_add(x, std::nu

关于风格的简单问题。这两种方法都有效(如下所述),我想知道哪种被认为是更好/更安全的做法? 我为添加提供了整数溢出保护,并为删除定义了一些功能。两种方法返回相同的结果

#include <iostream>
#include <limits>
void safe_add(int&, int);
void safe_remove(int&, int);

int main() {
    int x = 50;
    safe_add(x, std::numeric_limits<int>::max());
    return 0;
}

/* 
 * Adds two integers while protecting against overflow.
 * int& target is manipulated directly in this function
 */
void safe_add(int& target, int amount) {
    if(target > 0 && amount > std::numeric_limits<int>::max() - target) {
        target = std::numeric_limits<int>::max();
    } else if(amount > 0) { //a pos int that won't cause an overflow!
        target += amount;
    }
}

/* 
 * Removes "amount" from "target". This function protects
 * against target becoming a negative.
 * int& target is manipulated directly in this function.
 */
void safe_remove(int& target, int amount) {
    if (amount > 0){
        if (target >= amount) { 
            target -= amount; //guaranteed >= 0
        }
        else if (target < amount) {
            target = 0; //simply remove the rest, i don't want negatives!
        }
    }
}

你能打破任何一个吗?我错过了一些明显的东西吗?

因为您没有为函数提供文档/注释-从某人的角度来看,函数现在的样子,那么第一种方法就更清楚了,参考表明参数可以修改。在第二种情况下,人们并不确定返回的是什么

但这只是一种观点,你的问题无法客观地回答,因为“安全”对不同的人意味着不同


就我个人而言,我不会像您那样花那么多精力去做这件事,相反,我会专注于使用一种数据类型,该数据类型的大小由cstdint定义得很清楚。
int destination=left;安全添加(目的地,右侧)
int destination=safe_add(左、右)相反?@πάνταῥεῖ: 这些似乎是(有车)饱和操作,以防止意外溢出。你认为还有其他解释吗?(可能有,但我看不出)在不学究气的情况下,我所说的“安全”是指在实践中哪个更好?如果车坏了,介意指出在哪里吗,这样我就能修好它了?是的,@MooingDuck,这正是我的意思。一个比另一个干净吗?如果你不想要否定,考虑使用<代码>无符号int ;这让你的“安全”逻辑更简单了。哦,对不起,我会添加一些评论,让未来的读者更清楚地了解。谢谢你的洞察力!:)
#include <iostream>
#include <limits>
int safe_add(int, int);
int safe_remove(int, int);

int main() {
    int x = 50;
    x = safe_add(x, std::numeric_limits<int>::max());
    return 0;
}

/* 
 * Returns the result of two integers added together,
 * while protecting against int overflow.
 * No parameters passed to this function are manipulated.
 * Instead, an int should be assigned the result of this function.
 */
int safe_add(int target, int amount) {
    if(target > 0 && amount > std::numeric_limits<int>::max() - target) {
        target = std::numeric_limits<int>::max();
    } else if(amount > 0) {
        target += amount;
    }
    return target;//not affected if given a negative
}

/* 
 * Returns the result of "target - amount"
 * The lowest value that target can be is 0.
 * No parameters passed to this function are manipulated.
 * Instead, an int should be assigned the result of this function.
 */
int safe_remove(int target, int amount) {
    if (amount > 0){
        if (target >= amount) {
            target -= amount;
        }
        else if (target < amount) {
            target = 0;
        }
    }
    return target; //not affected if given a negative
}