跟我学C++中级篇—C++17中的元编程逻辑操作

一、逻辑操作

在C++中,逻辑运算符算是最常见的一种运算符,如&&,||以及!。这三种逻辑运算符对于处理条件判断和循环控制等有着重要的作用。说的更简单一些,就是处理程序的分支路径。这也符合现实世界中的工作处理实践。毕竟,计算机程序本来的目的就是为了解决现实世界中的问题。

二、元编程的逻辑操作

随着泛型编程特别是其中的元编程的发展,在编译期进行计算的需求也成为了一个重要的方向。&&这类逻辑运算符一般是只能用于运行时的逻辑操作,虽然在某些情况下(如编译时的常量表达式的逻辑运算)也可以使用。但在更多的情况下还是在运行时进行处理逻辑操作。

理论上讲,把它们扩展到编译期也不是不可能的。但是,从设计原则上和发展的眼光来看,这是一种非常不合理的设计。所以在C++17中,针对元编程中的逻辑操作给出了相应的三个逻辑操作的模板即:std::conjunction、std::disjunction和std::negation。即逻辑与,逻辑或和逻辑非。

三、std::conjunction、std::disjunction和std::negation

下面将针对这三个逻辑模板进行分析:

  1. std::conjunction
    逻辑与的操作,它接受N个布尔类型如前面提到的std::true_type或 std::false_type,它同样具有短路的行为,一量遇到std::false_type,将会终止模板类型的实例化。其也提供了 std::conjunction_v这种辅助变量模板。
c 复制代码
#include <iostream>
#include <type_traits>
 
// func is enabled if all Ts... have the same type as T
template<typename T, typename... Ts>
std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>
func(T, Ts...)
{
    std::cout << "All types in pack are the same.\n";
}
 
// otherwise
template<typename T, typename... Ts>
std::enable_if_t<!std::conjunction_v<std::is_same<T, Ts>...>>
func(T, Ts...)
{
    std::cout << "Not all types in pack are the same.\n";
}
 
template<typename T, typename... Ts>
constexpr bool all_types_are_same = std::conjunction_v<std::is_same<T, Ts>...>;
 
static_assert(all_types_are_same<int, int, int>);
static_assert(not all_types_are_same<int, int&, int>);
 
int main()
{
    func(1, 2, 3);
    func(1, 2, "hello!");
}
  1. std::disjunction
    逻辑或操作,它提供对多个布尔类型的执行或操作,同样它也支持短路行为,一但有一个std::true_type,后续将不再进行模板的实例化。其同样提供了disjunction_v这个辅助模板。
c 复制代码
#include <cstdint>
#include <string>
#include <type_traits>
 
// values_equal<a, b, T>::value is true if and only if a == b.
template<auto V1, decltype(V1) V2, typename T>
struct values_equal : std::bool_constant<V1 == V2>
{
    using type = T;
};
 
// default_type<T>::value is always true
template<typename T>
struct default_type : std::true_type
{
    using type = T;
};
 
// Now we can use disjunction like a switch statement:
template<int I>
using int_of_size = typename std::disjunction< //
    values_equal<I, 1, std::int8_t>,           //
    values_equal<I, 2, std::int16_t>,          //
    values_equal<I, 4, std::int32_t>,          //
    values_equal<I, 8, std::int64_t>,          //
    default_type<void>                         // must be last!
    >::type;
 
static_assert(sizeof(int_of_size<1>) == 1);
static_assert(sizeof(int_of_size<2>) == 2);
static_assert(sizeof(int_of_size<4>) == 4);
static_assert(sizeof(int_of_size<8>) == 8);
static_assert(std::is_same_v<int_of_size<13>, void>);
 
// checking if Foo is constructible from double will cause a hard error
struct Foo
{
    template<class T>
    struct sfinae_unfriendly_check { static_assert(!std::is_same_v<T, double>); };
 
    template<class T>
    Foo(T, sfinae_unfriendly_check<T> = {});
};
 
template<class... Ts>
struct first_constructible
{
    template<class T, class...Args>
    struct is_constructible_x : std::is_constructible<T, Args...>
    {
        using type = T;
    };
 
    struct fallback
    {
        static constexpr bool value = true;
        using type = void; // type to return if nothing is found
    };
 
    template<class... Args>
    using with = typename std::disjunction<is_constructible_x<Ts, Args...>...,
                                           fallback>::type;
};
 
// OK, is_constructible<Foo, double> not instantiated
static_assert(std::is_same_v<first_constructible<std::string, int, Foo>::with<double>,
                             int>);
 
static_assert(std::is_same_v<first_constructible<std::string, int>::with<>, std::string>);
static_assert(std::is_same_v<first_constructible<std::string, int>::with<const char*>,
                             std::string>);
static_assert(std::is_same_v<first_constructible<std::string, int>::with<void*>, void>);
 
int main() {}
  1. std::negation
    逻辑非操作,主要用于对模板参数B进行逻辑否定。同样也有negation_v辅助模板。
c 复制代码
#include <type_traits>
 
static_assert(
    std::is_same<
        std::bool_constant<false>,
        typename std::negation<std::bool_constant<true>>::type>::value,
    "");
static_assert(
    std::is_same<
        std::bool_constant<true>,
        typename std::negation<std::bool_constant<false>>::type>::value,
    "");
 
static_assert(std::negation_v<std::bool_constant<true>> == false);
static_assert(std::negation_v<std::bool_constant<false>> == true);
 
int main() {}

*上述代码均来自cppreference的示例代码

它们三个主要可以用于以下场景:

  1. 条件控制和处理
    可以与 std::enable_if_t或std::find_if等进行条件编译、判断和分支控制,实现SFINAE的处理。如:
c 复制代码
template<typename T>
std::enable_if_t<
    std::conjunction_v<
        std::is_integral<T>,
        std::is_signed<T>
    >, void>
 Check(T value) {
        // 控制重载
}
  1. 复杂类型或自定义类型
    可以通过其实现复杂类型的判断或自定义实现相关的数据类型,如:
c 复制代码
template<typename T>
using is_owner_integral = std::conjunction<
    std::is_integral<T>,
    std::is_signed<T>
>;

包括模板的特化处理等,都可以通过其来控制

  1. 编译错误控制和接口约束

通过逻辑判断(包括static_assert),如短路等来避免对更深层的复杂模板编译处理

四、两类逻辑操作的区别

在上面的分析中,发现二者虽然都可以实现逻辑的操作,但在具体应用中还是有所不同,主要包括:

  1. 应用时期不同
    元编程中的逻辑操作用于编译期;而逻辑运算符则在运行期应用(虽然在特定情况下逻辑操作运算符也可以用在编译期)
  2. 面向的操作对象不同
    元编程的逻辑操针对类型(类型特征模板);而逻辑运算符针对的布尔值
  3. 运算结果不同
    元编程的逻辑操作返回的是std::true_type等类型;而逻辑运算符返回的是布尔值
  4. 面对场景不同
    元编程的逻辑操作主要用于模板元编程、SFINAE等编译期的条件控制;而逻辑运算符则用于逻辑判断
  5. 性能和复杂度
    元编程的逻辑操作可能增加了编译时间,应用较为复杂,但提供了清晰可读性语法;而逻辑运算符直接编译成机器指令在运行时操作,开销低

五、总结

C++语言也是不断的发展和完善的,随着元编程技术被标准不断推进,相关的配套模块也会不断的充实和完善。这就需要开发者不断的跟进标准的发展,不断的学习并及时的整合到自己的知识体系中。

相关推荐
HABuo2 小时前
【Linux进程(五)】进程地址空间深入剖析-->虚拟地址、物理地址、逻辑地址的区分
linux·运维·服务器·c语言·c++·后端·centos
AuroraWanderll2 小时前
类和对象(六)--友元、内部类与再次理解类和对象
c语言·数据结构·c++·算法·stl
Tim_102 小时前
【C++入门】05、复合类型-数组
开发语言·c++·算法
jikiecui2 小时前
信奥崔老师:三目运算 (Ternary Operator)
数据结构·c++·算法
无限进步_2 小时前
【C语言&数据结构】另一棵树的子树:递归思维的双重奏
c语言·开发语言·数据结构·c++·算法·github·visual studio
汉克老师2 小时前
GESP2025年9月认证C++一级真题与解析(判断题1-10)
c++·数据类型·累加器·循环结构·gesp一级·gesp1级
不爱吃糖的程序媛2 小时前
OpenHarmony跨端生态适配全指南|Flutter/RN/三方库/C/C++/仓颉 鸿蒙化最佳实践
c语言·c++·flutter
fqbqrr2 小时前
2601C++,模块3
c++
坤虫debug3 小时前
C++ static_cast 解析:零成本的类型安全转换
c++