C++函数对象-运算符函数对象 - 逻辑运算 - 实现 x && y 的函数对象 (std::logical_and)

任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。

运算符函数对象

C++ 针对常用的算术和逻辑运算定义了很多函数对象:

逻辑运算

实现 x && y 的函数对象 

std::logical_and

template< class T >
struct logical_and;

(C++14 前)

template< class T = void >
struct logical_and;

(C++14 起)

进行逻辑与(逻辑合取)的函数对象。等效地调用类型 T 上的 operator&& 。

特化

标准库提供 std::logical_and 在不指定 T 时的特化,它使得参数类型和返回类型留待推导。

logical_and

实现 x && y 并推导参数和返回类型的函数对象
(类模板特化)
(C++14 起)

成员类型

类型 定义
result_type(C++17 中弃用) bool
first_argument_type(C++17 中弃用) T
second_argument_type(C++17 中弃用) T
(C++20 前)

成员函数

operator()

返回二个参数的逻辑与
(公开成员函数)

std::logical_and::operator()

bool operator()( const T& lhs, const T& rhs ) const;

(C++14 前)

constexpr bool operator()( const T& lhs, const T& rhs ) const;

(C++14 起)

返回 lhsrhs 的逻辑与。

参数

lhs, rhs - 要计算其逻辑与的值

返回值

lhs && rhs 的结果。

异常

(无)

可能的实现

constexpr bool operator()(const T &lhs, const T &rhs) const 
{
    return lhs && rhs;
}

调用示例

#include 
#include 

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
    Cell(const Cell &cell)
    {
        x = cell.x;
        y = cell.y;
    }

    Cell &operator+(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator+=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator*=(int n)
    {
        x *= n;
        y *= n;
        return *this;
    }

    Cell &operator++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    friend Cell operator +(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = cell1;
        cell += cell2;
        return cell;
    }

    friend Cell operator *(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};
        return cell;
    }

    friend Cell operator /(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};
        return cell;
    }

    friend Cell operator %(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};
        return cell;
    }

    friend bool operator ==(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x == cell2.x && cell1.y == cell2.y;
    }

    friend bool operator !=(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x != cell2.x && cell1.y != cell2.y;
    }

    friend bool operator <(const Cell &cell1, const Cell &cell2)
    {
        if (cell1.x == cell2.x)
        {
            return cell1.y < cell2.y;
        }
        else
        {
            return cell1.x < cell2.x;
        }
    }

    friend bool operator >(const Cell &cell1, const Cell &cell2)
    {
        if (cell1.x == cell2.x)
        {
            return cell1.y > cell2.y;
        }
        else
        {
            return cell1.x > cell2.x;
        }
    }

    friend bool operator &&(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x && cell2.x && cell1.y && cell2.y;
    }

    friend bool operator ||(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x || cell2.x || cell1.y || cell2.y;
    }

    friend bool operator !(const Cell &cell)
    {
        return !(cell.x && cell.x);
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::cout << std::boolalpha;

    int *ptr = nullptr;
    std::cout << "std::logical_and()(ptr, nullptr):      "
              << std::logical_and()(ptr, nullptr) << std::endl;

    std::cout << "std::logical_and()(50, 2):             "
              << std::logical_and()(50, 2) << std::endl;
    std::cout << "std::logical_and()('a', 97):           "
              << std::logical_and()('a', 97) << std::endl;
    std::cout << "std::logical_and()(1023, 1024):         "
              << std::logical_and()(1023, 1024) << std::endl;
    std::cout << "std::logical_and()(1023, 1024):        "
              << std::logical_and()(1023, 1024) << std::endl;
    std::cout << "std::logical_and()(1023, 1024):   "
              << std::logical_and()(1023, 1024) << std::endl;

    std::cout << "std::logical_and()(1023, 1024):     "
              << std::logical_and()(8, 32) << std::endl;
    std::cout << "std::logical_and()(123, 456):      "
              << std::logical_and()(123, 456) << std::endl;
    std::cout << "std::logical_and()(101, 202):      "
              << std::logical_and()(101, 202) << std::endl;
    std::cout << "std::logical_and()(10230, 10240):  "
              << std::logical_and()(10230, 10240) << std::endl;

    std::cout << "std::logical_and()(1023, 1024):      "
              << std::logical_and()(8, 32) << std::endl;
    std::cout << "std::logical_and()(123, 456):       "
              << std::logical_and()(123, 456) << std::endl;
    std::cout << "std::logical_and()(101, 202):       "
              << std::logical_and()(101, 202) << std::endl;
    std::cout << "std::logical_and()(10230, 10240):   "
              << std::logical_and()(10230, 10240) << std::endl;

    std::cout << "std::logical_and()(3.14, 3.14):      "
              << std::logical_and()(3.14, 3.14) << std::endl;
    std::cout << "std::logical_and()(3.14, 3.14):       "
              << std::logical_and()(3.14, 3.14) << std::endl;
    std::cout << "std::logical_and()(3, 3):             "
              << std::logical_and()(3, 3) << std::endl;
    std::cout << "std::logical_and()(3.56, 3.14):       "
              << std::logical_and()(3.56, 3.14) << std::endl;
    std::cout << "std::logical_and()(3.14, 3.14):         "
              << std::logical_and()(3.34, 3.34) << std::endl;

    std::cout << "std::logical_and()(Cell{101, 101}, Cell{202, 202}):       "
              << std::logical_and()(Cell{101, 101}, Cell{202, 202}) << std::endl;

    //编译失败
//    std::cout << "std::logical_and()(\"I am a \", \"handsome programmer\"):"
//              << std::logical_and()("I am a ", "handsome programmer") << std::endl;
    return 0;
}

输出

std::logical_and()(ptr, nullptr):      false
std::logical_and()(50, 2):             true
std::logical_and()('a', 97):           true
std::logical_and()(1023, 1024):         true
std::logical_and()(1023, 1024):        true
std::logical_and()(1023, 1024):   true
std::logical_and()(1023, 1024):     true
std::logical_and()(123, 456):      true
std::logical_and()(101, 202):      true
std::logical_and()(10230, 10240):  true
std::logical_and()(1023, 1024):      true
std::logical_and()(123, 456):       true
std::logical_and()(101, 202):       true
std::logical_and()(10230, 10240):   true
std::logical_and()(3.14, 3.14):      true
std::logical_and()(3.14, 3.14):       true
std::logical_and()(3, 3):             true
std::logical_and()(3.56, 3.14):       true
std::logical_and()(3.14, 3.14):         true
std::logical_and()(Cell{101, 101}, Cell{202, 202}):       true

你可能感兴趣的:(#,运算符函数对象,c++,标准库模板,STL,函数对象,逻辑运算,logical_and)