空格作为文字类型? [英] Void as a literal type?
问题描述
在C ++ 14 void
是一个文字类型
是一个文字类型,如果它是:
- void;或
- 标量类型;或
- 引用类型;或
- 文字类型数组;或
- 类类型(第9条),它具有以下所有属性: -
它有一个简单的析构函数,
- 它是一个聚合类型(8.5.1)或至少有一个constexpr
构造函数或构造函数模板,不是复制或移动
构造函数,
- 其所有非静态数据成员和基类都是
非易失性文字类型。
在C ++ 11 void
不是文字类型
一个类型是一个文字类型,如果它是:
- 一个标量类型;或
- 引用类型的引用类型;或
- 文字类型数组;或
- 类类型(第9条),它具有以下所有属性: -
它有一个简单的析构函数,
- 非静态数据成员(如果有)的
括号或等于初始化器中的每个构造函数调用和完全表达式是一个
常量表达式(5.19) ,
- 它是一个聚合类型(8.5.1)或至少有一个constexpr
不是复制或移动的构造函数或构造函数模板
构造函数和
- 其所有非静态数据成员和基类都是字面的
类型。
那么为什么 void
一个文字类型?
由于 void
是字面类型
, constexpr
函数在C ++中可以有返回类型 void
14。
p>允许任意的表达式语句,以允许
调用执行检查的函数,并允许类似asb的
构造。 void也会成为一个文字类型,因此constexpr
函数只有在执行此类检查时才会返回void。
b $ b
#define ASSERT(expr)\
(void)((expr)|| assert_failed(#expr,__LINE__,__FILE__))
void assert_failed(...); // not constexpr
struct S {
std :: array a< int,100> ;;
size_t i;
constexpr void check_invariants()const {
ASSERT(i< a.size());
ASSERT(a [i] == 0);
}
S(std :: array a_,size_t i_):a(a_),i(i_){
check_invariants
}
};
In C++14 void
is a literal type
A type is a literal type if it is:
— void; or
— a scalar type; or
— a reference type; or
— an array of literal type; or
— a class type (Clause 9) that has all of the following properties: — it has a trivial destructor,
— it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
— all of its non-static data members and base classes are of non-volatile literal types.
In C++11 void
is not a literal type
A type is a literal type if it is:
— a scalar type; or
— a reference type referring to a literal type; or
— an array of literal type; or
— a class type (Clause 9) that has all of the following properties: — it has a trivial destructor,
— every constructor call and full-expression in the brace-or-equal-initializers for non-static data members (if any) is a constant expression (5.19),
— it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
— all of its non-static data members and base classes are of literal types.
So why is void
a literal type? What benefits does it offer?
Since void
is literal type
, constexpr
functions can have return type void
in C++14.
It's covered in this proposal.
Quote from proposal:
An arbitrary expression-statement is permitted, in order to allow calls to functions performing checks and to allow assert-like constructs. void also becomes a literal type, so that constexpr functions which exist only to perform such checks may return void.
#define ASSERT(expr) \
(void)((expr) || assert_failed(#expr, __LINE__, __FILE__))
void assert_failed(...); // not constexpr
struct S {
std::array a<int, 100>;
size_t i;
constexpr void check_invariants() const {
ASSERT(i < a.size());
ASSERT(a[i] == 0);
}
S(std::array<int, 100> a_, size_t i_) : a(a_), i(i_) {
check_invariants();
}
};
这篇关于空格作为文字类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!