定制制作网站价格表wordpress sql查询分类
定制制作网站价格表,wordpress sql查询分类,昨晚贵州出大事,门户网站的概念1 type_traits 的概述
type_traits 是 C 标准模板库#xff08;STL#xff09;中的一个头文件#xff0c;它定义了一系列模板类#xff0c;这些模板类在编译期获取某一参数、某一变量、某一个类等的类型信息#xff0c;主要用于进行静态检查。通过使用 type_traits#…1 type_traits 的概述
type_traits 是 C 标准模板库STL中的一个头文件它定义了一系列模板类这些模板类在编译期获取某一参数、某一变量、某一个类等的类型信息主要用于进行静态检查。通过使用 type_traits程序员可以在编译时就获得关于类型的详细信息从而可以在不实际运行程序的情况下进行类型相关的优化和检查。
type_traits 中的内容主要可以分为以下几类
辅助基类如 std::integral_constant 以及其特化 true_type 和 false_type这些类用于创建编译器常量同时也是类型萃取类模板的基类。类型萃取类模板这些模板类用于在编译期以常量的形式获取类型特征。例如std::is_integral 用于检查一个类型是否为整数类型std::is_floating_point 用于检查一个类型是否为浮点类型std::is_base_of 用于检查一个类型是否是另一个类型的基类等。这些模板类主要用来进行类型属性的判断。类型转换类模板这些模板类用于通过执行特定操作从已有类型获得新类型。例如std::add_const 用于给类型添加 const 限定符std::remove_volatile 用于移除类型的 volatile 限定符等。
type_traits 的主要作用包括
在编译期对类型进行属性检查和转换避免了运行时的类型检查和转换开销。使得模板编程更加灵活和安全可以在模板实例化之前根据类型属性选择不同的实现。为 STL 中的算法和容器提供了类型相关的支持例如STL 中的算法通过迭代器存取容器内容而 type_traits 可以协助算法根据迭代器类型或容器类型选择不同的策略。
在 C11 给出 type_traits 之前对于类型处理有很多非常不方便的地方
1类型信息的缺失 C 是一种静态类型语言这意味着在编译时每个变量和表达式都必须有明确的类型。然而标准 C 并没有提供一个直接的机制来获取和操作这些类型信息。在没有 type_traits 的情况下程序员通常需要手动跟踪和处理类型信息这既繁琐又容易出错。
2模板元编程的复杂性 C 的模板元编程是一种在编译期进行计算和类型操作的技术。然而由于缺乏 type_traits 这样的工具模板元编程变得异常复杂和难以维护。程序员需要手动编写大量的模板特化和递归代码以处理各种类型情况。这不仅增加了开发难度也降低了代码的可读性和可维护性。
13类型安全和泛型编程的限制 在泛型编程中我们通常需要编写能够处理多种类型的代码。然而在没有 type_traits 的情况下我们很难在编译期对类型进行严格的检查和约束。这可能导致类型不安全的代码例如错误地将一个浮点数传递给期望整数的函数。此外由于缺乏类型萃取和转换的工具泛型编程的灵活性也会受到限制。
4性能优化的挑战 在某些情况下我们需要根据类型信息来优化代码的性能。例如对于某些特定的类型我们可能希望使用特定的算法或数据结构。然而在没有 type_traits 的情况下这种优化变得非常困难。我们需要编写复杂的条件编译代码或者使用运行时类型信息RTTI来动态地选择实现。这些方法要么增加了编译的复杂性要么引入了额外的运行时开销。
type_traits 的出现极大地缓解了这些挑战。它提供了一套丰富的工具使得程序员可以在编译期获取和操作类型信息从而简化了模板元编程提高了类型安全性并使得性能优化变得更加容易。通过使用 type_traits可以编写更加灵活、安全和高效的 C 代码。
2 type_traits 核心类型特性
2.1 std::is_same
std::is_same 是一个模板类用于检查两个类型是否相同。其定义如下
template class T, class U
struct is_same;当 T 和 U 指名同一类型考虑 const 和 volatile 限定时is_sameT, U::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; // 输出 true 或 false 而不是 1 或 0 std::cout int and int are std::is_sameint, int::value \n; // 输出 true std::cout int and float are std::is_sameint, float::value \n; // 输出 false return 0;
}2.2 std::is_integral
std::is_integral 是一个模板类用于检查一个类型是否为整数类型。其定义如下
template class T
struct is_integral;当 T 为整数类型时is_integralT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout int is std::is_integralint::value \n; // 输出 true std::cout double is std::is_integraldouble::value \n; // 输出 false return 0;
}2.3 std::is_floating_point
std::is_floating_point 是一个模板类用于检查一个类型是否为浮点类型。其定义如下
template class T
struct is_floating_point;当 T 为 float、double 或 long double包括任何 cv 限定变体时is_floating_pointT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout float is std::is_floating_pointfloat::value \n; // 输出 true std::cout int is std::is_floating_pointint::value \n; // 输出 false return 0;
}2.4 std::is_array
std::is_array 是一个模板类用于检查一个类型是否为数组类型。其定义如下
template class T
struct is_array;当 T 为数组类型时is_arrayT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout int[5] is std::is_arrayint[5]::value \n; // 输出 true std::cout int is std::is_arrayint::value \n; // 输出 false return 0;
}2.5 std::is_pointer
std::is_pointer 是一个模板类用于检查一个类型是否为指针类型。其定义如下
template class T
struct is_pointer;当 T 为指针类型时is_pointerT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() {std::cout std::boolalpha;std::cout int* is std::is_pointerint*::value \n; // 输出 true std::cout int is std::is_pointerint::value \n; // 输出 false return 0;
}2.6 std::is_reference
std::is_reference 是一个模板类用于检查一个类型是否为引用类型。其定义如下
template class T
struct is_reference;当 T 为引用类型时is_referenceT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout int is std::is_referenceint::value \n; // 输出 true std::cout int is std::is_referenceint::value \n; // 输出 false return 0;
}2.7 std::is_void
std::is_void 是一个模板类用于检查一个类型是否为 void 类型。其定义如下
template class T
struct is_void;当 T 为 void 类型时is_voidT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout void is std::is_voidvoid::value \n; // 输出 true std::cout int is std::is_voidint::value \n; // 输出 false return 0;
}2.8 std::is_const
std::is_const 是一个模板类用于检查一个类型是否为常量类型。其定义如下
template class T
struct is_const;当 T 为常量类型时is_constT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout const int is std::is_constconst int::value \n; // 输出 true std::cout int is std::is_constint::value \n; // 输出 false return 0;
}2.9 std::is_volatile
std::is_volatile 是一个模板类用于检查一个类型是否为易变类型。其定义如下
template class T
struct is_volatile;当 T 为易变类型时is_volatileT::value 为 true否则为 false。
样例
#include iostream
#include type_traits int main() { std::cout std::boolalpha; std::cout volatile int is std::is_volatilevolatile int::value \n; // 输出 true std::cout int is std::is_volatileint::value \n; // 输出 false return 0;
}2.10 其他类型
除了上面介绍的类型type_traits 还有如 std::is_signed 检查类型是否有符号、std::is_unsigned 检查类型是否无符号、std::is_arithmetic 检查类型是否为算术类型整数或浮点数等基础核心类型具体的可以查看 C 标准中的类型说明。也可以通过查看头文件 type_traits 获得。
3 使用核心类型特性实现泛型编程中的条件编译
在 C11 中可以使用模板特化或者 std::enable_if 来实现类似条件编译的效果。下面是一个使用 std::is_same 和 std::is_integral 以及 std::enable_if 来实现泛型编程中条件编译的实例
#include iostream
#include type_traits // 泛型函数模板用于非整数类型
templatetypename T, typename void
void print_type_info(T value, typename std::enable_if!std::is_integralT::value::type* nullptr) {std::cout Value is of a non-integral type. std::endl;
}// 对 int 类型特化的版本
templatetypename T
void print_type_info(T value, typename std::enable_ifstd::is_sameT, int::value::type* nullptr) {std::cout Value is of type int with value: value std::endl;
}// 对整数类型特化的版本但排除 int
templatetypename T
void print_type_info(T value, typename std::enable_ifstd::is_integralT::value !std::is_sameT, int::value::type* nullptr) {std::cout Value is of an integral type (non-int) with value: value std::endl;
}int main() {// 调用 print_type_info 模板函数传入不同类型的值 print_type_info(12); // 调用 int 特化的版本 print_type_info(3.14f); // 调用非整数类型特化版本 print_type_info(static_castlong long(1234567890123456789)); // 调用整数类型特化的版本排除 int return 0;
}上面代码的输出为
Value is of type int with value: 12
Value is of a non-integral type.
Value is of an integral type (non-int) with value: 1234567890123456789这个例子定义了三个版本的 print_type_info 函数模板
一个非特化的版本它接受非整数类型的参数 T并打印一个通用消息。一个对 int 类型特化的版本它只接受 int 类型的参数并打印 int 类型的特定消息。一个对整数类型除了 int特化的版本它接受任何整数类型除了 int的参数并打印整数类型的特定消息。
std::enable_if 用于在编译时启用或禁用模板函数的不同特化版本。std::enable_if 的第二个模板参数是一个类型当该类型存在时模板会被启用当该类型为 void 时模板会被禁用。std::is_sameT, int::value 和 std::is_integralT::value 用于在编译时检查类型。
4 使用核心类型特性实现类型安全的容器
下面是一个简单的类型安全整数容器的例子它只允许存储整数类型并且可以使用 std::is_same 来确保不添加与容器定义类型相同的类型
#include iostream
#include type_traits
#include vector
#include stdexcept // 类型安全的整数容器
templatetypename T
class TypeSafeIntegerContainer {
public:static_assert(std::is_integralT::value, TypeSafeIntegerContainer can only hold integral types.);// 添加元素到容器中 templatetypename U, typename std::enable_ifstd::is_integralU::value !std::is_sameU, T::valuevoid add(U value) {// 检查新类型是否与 T 类型相同 static_assert(!std::is_sameU, T::value, Cannot add the same type to the container.);data.push_back(static_castT(value));}// 获取容器中所有元素的数量 size_t size() const {return data.size();}// 打印容器中所有元素的值 void print() const {for (const auto elem : data) {std::cout elem ;}std::cout std::endl;}private:std::vectorT data; // 存储整数类型的容器
};int main()
{TypeSafeIntegerContainerint intContainer;// 添加不同类型的整数到容器中 intContainer.add(10L); // OK, long is converted to int intContainer.add(20LL); // OK, long long is converted to int intContainer.add(30U); // OK, unsigned int is converted to int // 尝试添加相同类型的整数会导致编译错误 // intContainer.add(40); // 编译错误: Cannot add the same type to the container. // 打印容器中的元素 intContainer.print(); // 输出: 10 20 30 // 尝试创建一个存储非整数类型的容器会导致编译错误 // TypeSafeIntegerContainerstd::string stringContainer; // 编译错误: TypeSafeIntegerContainer can only hold integral types. return 0;
}上面代码的输出为
10 20 30在这个例子中TypeSafeIntegerContainer 是一个模板类它接受一个整数类型 T 作为模板参数。它有一个 add 成员函数模板该函数模板只接受与 T 不同且为整数类型的参数。样例使用 std::enable_if_t 和 std::is_integral 来确保这一点。
这个例子还使用 static_assert 来确保在实例化 TypeSafeIntegerContainer 时T 是一个整数类型以及在调用 add 方法时新类型 U 不与 T 相同。如果尝试添加与 T 类型相同的类型或者尝试存储非整数类型都会导致编译错误。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88189.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!