菜鸟笔记
提升您的技术认知

C++17新特性——std::optional

一、前言

我们在写代码过程中可能经常会碰到这样的情况:

某个函数经过一系列计算后获取一个返回值,但是这个函数可能在执行过程中有异常分支,从这些异常分支中return出来的时候还没有得到这个想计算的值。  例如想计算得到一个无符号类型,我们可能会在这些异常分支return一个-1出去,用来表示计算失败,或执行有误,甚至不同的负值用来表示不同的异常情况。。。由于这些值的存在,本来返回值应该是uint32类型,为了容纳可能出现的负值和整个uint32的范围,返回值就变成了int64。。。这个int64,竟然要么表示错误码,要么表示计算结果?这给阅读和使用上都造成了不便。此外,这种负值的出现还引入了魔鬼数字。

举个栗子:(伪代码,并不能跑,只是用作举例)

#include <cstdint>

int64_t getUintValue() {
    if (xxx) {
        return -1; // malloc fail
    }
    if (xxx) {
        return -2; // invalid param
    }
    // do some work
    return someValue;
}

或者是一个计算布尔值的函数,失败时也返回false吗?调用者拿到这个值一脸懵逼。。

bool getBoolValue() {
    if (xxx) {
        return false;  // 计算失败时返回的false
    }
    // do some work
    return false;  // 计算成功时返回的false
}

还有一种情况发生在枚举中。通常为了应对枚举类型暂时还不知道时,我们会新增一个枚举值叫UNKNOWN。这样以来,任何一个使用该枚举类型的地方都要首先判断是不是UNKNOWN,造成代码冗余。举个栗子:

enum Color {
    RED,
    BLUE,
    GREEN,
    UNKNOWN
};

void printColor(Color c) {
    // 这里还要不要判断是不是UNKNOWN?
    // 如果不判断,其它地方复用这个函数时怎么办?
}

void judgeColor(Color c) {
    if (c != UNKNOWN) {
        printColor(c);
    }
}

C++17新增了std::optional来解决这个问题。

二、optional的使用

(gcc7/clang4/msvc19.10以上才支持optional,首先先检查你的编译器版本)

optional是一个模板类:

template <class T>
class optional;

它内部有两种状态,要么有值(T类型),要么没有值(std::nullopt)。有点像T*指针,要么指向一个T类型,要么是空指针(nullptr)。

std::optional有以下几种构造方式:

#include <iostream>
#include <optional>
using namespace std;

int main() {
    optional<int> o1;  //什么都不写时默认初始化为nullopt
    optional<int> o2 = nullopt;  //初始化为无值
    optional<int> o3 = 10;  //用一个T类型的值来初始化
    optional<int> o4 = o3;  //用另一个optional来初始化
    return 0;
}

查看一个optional对象是否有值,可以直接用if,或者用has_value()

#include <iostream>
#include <optional>
using namespace std;

int main() {
    optional<int> o1;
    if (o1) {
        printf("o1 has value\n");
    }
    if (o1.has_value()) {
        printf("o1 has value\n");
    }
    return 0;
}

当一个optional有值时,可以通过用指针的方式(*号和->号)来使用它,或者用.value()拿到它的值:

#include <iostream>
#include <optional>
#include <string>
using namespace std;

int main() {
    optional<int> o1 = 5;
    printf("%d\n", *o1);
    optional<string> o2 = "hello";
    printf("%s\n", o2->c_str());
    printf("%s\n", o2.value().c_str());
    return 0;
}

将一个有值的optional变为无值,用.reset()。该函数会将已存储的T类型对象析构

#include <iostream>
#include <optional>
using namespace std;

int main() {
    optional<int> o1 = 5;
    o1.reset();
}

三、解决前言中的问题

于是在第一章中的3个问题可以这样改写:

#include <cstdint>
#include <optional>
using namespace std;

optional<uint32_t> getUintValue() {
    if (xxx) {
        return nullopt; // malloc fail
    }
    if (xxx) {
        return nullopt; // invalid param
    }
    // do some work
    return someValue;
}
#include <optional>
using namespace std;

optional<bool> getBoolValue() {
    if (xxx) {
        return nullopt;  // 计算失败时返回
    }
    // do some work
    return false;  // 计算成功时返回的false
}
#include <optional>
using namespace std;

enum Color {
    RED,
    BLUE,
    GREEN
};

void printColor(Color c) {
    // 这里永远都不需要判断这个值是否合法
}

void judgeColor(optional<Color> c) {
    if (c.has_value()) {
        printColor(c.value());
    }
}

可读性暴增!

四、自己实现一个optional

撸了一个极其简易的optional

#include <iostream>
#include <type_traits>
#include <memory>
#include <string>

using namespace std;

class NullOptType {
};

NullOptType nullOpt;

template<typename T, enable_if_t<!is_reference_v<T>> * = nullptr>
class Optional {
public:
    Optional() = default;

    virtual ~Optional() = default;

    Optional(NullOptType) {}

    Optional(const T &v) {
        m_data = make_unique<T>(v);
        m_hasValue = true;
    }

    Optional(const Optional &v) {
        if (v.HasValue()) {
            m_data = make_unique<T>(Value());
            m_hasValue = true;
        }
    }

    bool HasValue() const {
        return m_hasValue;
    }

    explicit operator bool() const {
        return m_hasValue;
    }

    const T &Value() const {
        if (m_hasValue) {
            return *m_data;
        } else {
            throw std::exception();
        }
    }

    const T &operator*() const {
        return Value();
    }

    void Reset() {
        m_data.reset();
        m_hasValue = false;
    }

private:
    bool m_hasValue = false;
    unique_ptr<T> m_data = nullptr;
};

int main() {
    Optional<int> o1;
    if (o1) {
        printf("o1 has value\n");
    }
    Optional<int> o2 = nullOpt;
    if (o2) {
        printf("o2 has value\n");
    }
    Optional<int> o3 = 10;
    if (o3) {
        printf("o3 has value %d\n", o3.Value());
    }
    Optional<string> o4 = string("haha");
    if (o4.HasValue()) {
        printf("o4 has value %s\n", (*o4).c_str());
    }
    //Optional<int&> o5; 编译失败
}