编程语言中的元函数和元类是什么意思?
What is mean by Metafunctions & Metaclasses in Programming Languages?
我在编译时 c++ 计算期间多次遇到这些术语。我在网上搜索过,我发现的资源中 "expert level" 东西我无法理解。请帮助理解这些条款?我正在寻找初学者级别的好解释。
非常感谢您的帮助!
普雷
元函数
C++ 中的元函数是一种使用模板元编程表达 compile-time 计算的方法——使用模板实例化和类型推导在编译时生成结果。
从根本上说,元函数是一个 class 模板,具有 constexpr
成员(对于返回值的元函数)或类型定义(对于返回类型的元函数)。
该技术可以通过元函数 is_same
来说明,它检查两个类型参数是否相同。一个可能的实现(来自cppreference.com)是
template<class T, class U>
struct is_same : std::false_type {};
template<class T>
struct is_same<T, T> : std::true_type {};
其中 std::true_type
是一个辅助元函数,它有一个成员 constexpr bool value = true
(false
代表 false_type
)。
通过实例化模板并读取包含结果的成员来调用元函数,例如表达式
is_same<int,int32_t>::value
如果 int
是 32 位,则计算结果为布尔值 true
,否则为 false
。
来自 type_traits
的另一个示例是 std::is_floating_point
,它检查类型是否为浮点类型。它可以被称为
is_floating_point<int>::value
标准库(大部分)约定元函数返回值具有成员 value
,元函数返回类型具有类型别名 type
.
type-returning 元函数的一个示例是 std::iterator_traits
,它用于获取有关迭代器的信息。例如,给定一个迭代器类型 Iter
,可以使用
获取值类型(即通过取消引用迭代器返回的类型)
iterator_traits<Iter>::value_type
和迭代器类别(e.i、ForwardIterator
、RandomAccessIterator
等)和
iterator_traits<Iter>::iterator_category
compile-time 计算的一个示例是计算阶乘的元函数:
template <unsigned int N>
struct Fac{
static constexpr unsigned int value = N * Fac<N-1>::value;
};
template <>
struct Fac<0>{
static constexpr unsigned int value = 1;
};
同样,这个元函数被称为 Fac<5>::value
元classes
Metaclasses 是对 C++ 的提议添加,以允许在代码中表达对(某种)class 的约束,而不仅仅是使用约定和文档。
例如,"interface" 通常用于描述具有
的 class
- 只有纯虚public函数
- 一个虚拟析构函数
有了 metaclass interface
,可以这样写
interface Example {
void Foo();
int Bar(int);
}
然后将由编译器实例化为 class
class Example {
public:
virtual void Foo() =0;
virtual int Bar(int) =0;
virtual ~Foo() =default;
}
可以在 fluentc++ blog post. A comprehensive source is Herb Sutter's blog post
中找到元class 提案的一个很好的总结
我在编译时 c++ 计算期间多次遇到这些术语。我在网上搜索过,我发现的资源中 "expert level" 东西我无法理解。请帮助理解这些条款?我正在寻找初学者级别的好解释。 非常感谢您的帮助! 普雷
元函数
C++ 中的元函数是一种使用模板元编程表达 compile-time 计算的方法——使用模板实例化和类型推导在编译时生成结果。
从根本上说,元函数是一个 class 模板,具有 constexpr
成员(对于返回值的元函数)或类型定义(对于返回类型的元函数)。
该技术可以通过元函数 is_same
来说明,它检查两个类型参数是否相同。一个可能的实现(来自cppreference.com)是
template<class T, class U>
struct is_same : std::false_type {};
template<class T>
struct is_same<T, T> : std::true_type {};
其中 std::true_type
是一个辅助元函数,它有一个成员 constexpr bool value = true
(false
代表 false_type
)。
通过实例化模板并读取包含结果的成员来调用元函数,例如表达式
is_same<int,int32_t>::value
如果 int
是 32 位,则计算结果为布尔值 true
,否则为 false
。
来自 type_traits
的另一个示例是 std::is_floating_point
,它检查类型是否为浮点类型。它可以被称为
is_floating_point<int>::value
标准库(大部分)约定元函数返回值具有成员 value
,元函数返回类型具有类型别名 type
.
type-returning 元函数的一个示例是 std::iterator_traits
,它用于获取有关迭代器的信息。例如,给定一个迭代器类型 Iter
,可以使用
iterator_traits<Iter>::value_type
和迭代器类别(e.i、ForwardIterator
、RandomAccessIterator
等)和
iterator_traits<Iter>::iterator_category
compile-time 计算的一个示例是计算阶乘的元函数:
template <unsigned int N>
struct Fac{
static constexpr unsigned int value = N * Fac<N-1>::value;
};
template <>
struct Fac<0>{
static constexpr unsigned int value = 1;
};
同样,这个元函数被称为 Fac<5>::value
元classes
Metaclasses 是对 C++ 的提议添加,以允许在代码中表达对(某种)class 的约束,而不仅仅是使用约定和文档。
例如,"interface" 通常用于描述具有
的 class- 只有纯虚public函数
- 一个虚拟析构函数
有了 metaclass interface
,可以这样写
interface Example {
void Foo();
int Bar(int);
}
然后将由编译器实例化为 class
class Example {
public:
virtual void Foo() =0;
virtual int Bar(int) =0;
virtual ~Foo() =default;
}
可以在 fluentc++ blog post. A comprehensive source is Herb Sutter's blog post
中找到元class 提案的一个很好的总结