c++ 中判断变量是否为 optional 的方法

#include <type_traits>
#include <optional>
#include <boost/optional.hpp>

namespace traits
{
// 特化模板结构体,继承自 std::false_type
template<class type>
struct is_optional : std::false_type {};

// 偏特化 std::optional,继承自 std::true_type
template<class type>
struct is_optional<std::optional<type>> : std::true_type {};

// 偏特化 boost::optional,继承自 std::true_type
template<class type>
struct is_optional<boost::optional<type>> : std::true_type {};

// 模板变量,call 时根据 type 选择合适的 is_optional 模板,返回对应的 value 值给 is_optional_v
// 如果 type 是 std::optional 或 boost::optional value 值是 true (因为对应的 is_optional,第2、3个实现,继承自 std::true_type)
// 如果 type 是其它类型 value 值是 false(因为对应的 is_optional,也就是第1个实现,继承自 std::false_type)
template<class type>
inline constexpr bool is_optional_v = is_opitional<type>::value;
// 其它类型像 set vector 也可用此方法进行判断
}
std::false_type std::true_type 相关源码
template <class _Tp, _Tp __v>
struct _LIBCPP_TEMPLATE_VIS integral_constant
{
  static _LIBCPP_CONSTEXPR const _Tp      value = __v;
  typedef _Tp               value_type;
  typedef integral_constant type;
  _LIBCPP_INLINE_VISIBILITY
  _LIBCPP_CONSTEXPR operator value_type() const _NOEXCEPT {return value;}
#if _LIBCPP_STD_VER >= 14
  _LIBCPP_INLINE_VISIBILITY
  constexpr value_type operator ()() const _NOEXCEPT {return value;}
#endif
};

template <class _Tp, _Tp __v>
_LIBCPP_CONSTEXPR const _Tp integral_constant<_Tp, __v>::value;

typedef integral_constant<bool, true>  true_type;
typedef integral_constant<bool, false> false_type;
is_optional_v 使用示例

假设有 left、right 两个值,如果 left != right,就把 right 值赋给 left。
对应实现如下:

struct Left
{
    int a = 1;
    std::optional<int> b = 8;
    boost::optional<std::string> c = boost::none;
};

struct Right
{
    int a = 5;
    std::optional<int> b;
    boost::optional<std::string> c = std::string("test");
};

template <typename T>
bool assign(T& left, const T& right) {
    // 如果 T 是 optional,is_opitional<T>::value 的值返回 true,is_optional_v 值就是 true
    if constexpr (traits::is_optional_v<T>) {
        if (right && right != left) {
            left = right;
            return true;
        }
    }
    // 如果 T 是 optional 外的其它类型,则走此分支
    else {
        if (right != left) {
            left = right;
            return true;
        }
    }
    return false;
}

bool updateLeftBaseOnRight(Left left, const Right& right)
{
    bool ret = false;
    ret |= assign(left.a, right.a);
    ret |= assign(left.b, right.b);
    ret |= assign(left.c, right.c);
    return ret;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容