gpt4 book ai didi

c++ - 在 C++ 中包装算术类型

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:47 24 4
gpt4 key购买 nike

C++ 很棒,但您不能从算术类型继承,有时这很有用。我写了以下内容:

template <typename type> class arithmetic
{
static_assert(std :: is_arithmetic <type> :: value, "Please provide an arithmetic type.");

// Members

type _value;

public:

// Constructors

inline arithmetic() = default;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline arithmetic(const rtype &);

// Arithmetic operators

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator + (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator - (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator * (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator / (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator % (const rtype &) const;

inline auto operator + () const;
inline auto operator - () const;

inline auto operator ++ ();
inline auto operator ++ (int);

inline auto operator -- ();
inline auto operator -- (int);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator = (const rtype &);

// Comparison operators

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator == (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator != (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator > (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator < (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator >= (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator <= (const rtype &) const;

// Logical operators

inline auto operator ! () const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator && (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator || (const rtype &) const;

// Bitwise operators

inline auto operator ~ () const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator & (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator | (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator ^ (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator << (const rtype &) const;

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator >> (const rtype &) const;

// Compound assignment operators

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator += (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator -= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator *= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator /= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator %= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator &= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator |= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator ^= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator <<= (const rtype &);

template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type * = nullptr> inline auto operator >>= (const rtype &);

// Member and pointer operators

inline type * operator & ();
inline const type * operator & () const;

// Casting

inline operator type & ();
inline operator const type & () const;
};

// Constructors

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline arithmetic <type> :: arithmetic(const rtype & value) : _value(value)
{
}

// Arithmetic operators

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator + (const rtype & rvalue) const
{
return this->_value + rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator - (const rtype & rvalue) const
{
return this->_value - rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator * (const rtype & rvalue) const
{
return this->_value * rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator / (const rtype & rvalue) const
{
return this->_value / rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator % (const rtype & rvalue) const
{
return this->_value % rvalue;
}

template <typename type> inline auto arithmetic <type> :: operator + () const
{
return +(this->_value);
}

template <typename type> inline auto arithmetic <type> :: operator - () const
{
return -(this->_value);
}

template <typename type> inline auto arithmetic <type> :: operator ++ ()
{
return ++(this->_value);
}

template <typename type> inline auto arithmetic <type> :: operator ++ (int)
{
return (this->_value)++;
}

template <typename type> inline auto arithmetic <type> :: operator -- ()
{
return --(this->_value);
}

template <typename type> inline auto arithmetic <type> :: operator -- (int)
{
return (this->_value)++;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator = (const rtype & rvalue)
{
return this->_value = rvalue;
}

// Comparison operators

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator == (const rtype & rvalue) const
{
return this->_value == rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator != (const rtype & rvalue) const
{
return this->_value != rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator > (const rtype & rvalue) const
{
return this->_value > rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator < (const rtype & rvalue) const
{
return this->_value < rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator >= (const rtype & rvalue) const
{
return this->_value >= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator <= (const rtype & rvalue) const
{
return this->_value <= rvalue;
}

// Logical operators

template <typename type> inline auto arithmetic <type> :: operator ! () const
{
return !(this->_value);
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator && (const rtype & rvalue) const
{
return this->_value && rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator || (const rtype & rvalue) const
{
return this->_value || rvalue;
}

// Bitwise operators

template <typename type> inline auto arithmetic <type> :: operator ~ () const
{
return ~(this->_value);
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator & (const rtype & rvalue) const
{
return this->_value & rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator | (const rtype & rvalue) const
{
return this->_value | rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator ^ (const rtype & rvalue) const
{
return this->_value ^ rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator << (const rtype & rvalue) const
{
return this->_value << rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator >> (const rtype & rvalue) const
{
return this->_value >> rvalue;
}

// Compound assignment operators

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator += (const rtype & rvalue)
{
return this->_value += rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator -= (const rtype & rvalue)
{
return this->_value -= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator *= (const rtype & rvalue)
{
return this->_value *= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator /= (const rtype & rvalue)
{
return this->_value /= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator %= (const rtype & rvalue)
{
return this->_value %= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator &= (const rtype & rvalue)
{
return this->_value &= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator |= (const rtype & rvalue)
{
return this->_value |= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator ^= (const rtype & rvalue)
{
return this->_value ^= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator <<= (const rtype & rvalue)
{
return this->_value <<= rvalue;
}

template <typename type> template <typename rtype, typename std :: enable_if <__arithmetic :: __is_arithmetic_convertible <rtype> :: value> :: type *> inline auto arithmetic <type> :: operator >>= (const rtype & rvalue)
{
return this->_value >>= rvalue;
}

// Member and pointer operators

template <typename type> inline type * arithmetic <type> :: operator & ()
{
return &(this->_value);
}

template <typename type> inline const type * arithmetic <type> :: operator & () const
{
return &(this->_value);
}

// Casting

template <typename type> inline arithmetic <type> :: operator type & ()
{
return this->_value;
}

template <typename type> inline arithmetic <type> :: operator const type & () const
{
return this->_value;
}

这基本上只是一个非常迂腐的算术类型包装器。包装器有一个名为 _value 的算术成员, 然后所有对任何运营商的调用都被转发到 _value , 并且有一个原始算术类型的转换运算符。

现在,我想知道,是否存在任何情况,例如 arithmetic <int>int 的行为不同?我似乎无法弄清楚,但我想我会问一个更明智的意见。

另一方面,如果这按预期工作并且arithmetic <int>表现为 int ,那为什么这部分不是标准呢?它看起来很容易实现,并且允许我们随心所欲地扩展算术类型。

最佳答案

最重要的一个问题 - 在隐式转换序列中不超过一个用户定义的转换。考虑:

class C { C(int); };
void f(C);

f(42); // works, calls f(C(42));
f(arithmetic<int>(42)); // wouldn't work.

另一个问题 - 模板特化:

template <typename T> void f(T) { std::cout << "Generic"; }
template <> void f<int>(int) { std::cout << "Specialized"; }

f(42); // calls specialized
f(arithmetic<int>(42)); // calls generic

言归正传——可以用arithmetic<int>但不是 arithmetic<arithmetic<int>> .更一般地说,各种模板元编程技术能够分辨出差异。

关于c++ - 在 C++ 中包装算术类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38101333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com