constexpr和const之间有什么区别?
我什么时候只能使用其中一个?我什么时候可以同时使用这两种方法,我应该如何选择一种?
constexpr和const之间有什么区别?
我什么时候只能使用其中一个?我什么时候可以同时使用这两种方法,我应该如何选择一种?
当前回答
const和constexpr关键字概述
在C++中,如果用常量表达式初始化常量对象,我们可以在需要常量表达式的地方使用常量对象。
const int x = 10;
int a[x] = {0};
例如,我们可以在switch中使用case语句。
constexpr可以与数组一起使用。
constexpr不是类型。
constexpr关键字可以与auto关键字一起使用。
constexpr auto x = 10;
struct Data { // We can make a bit field element of struct.
int a:x;
};
如果我们用常量表达式初始化常量对象,那么该常量对象生成的表达式现在也是常量表达式。
常量表达式:可以在编译时计算其值的表达式。
x*5-4//这是一个常量表达式。对于编译器,键入此表达式和直接键入46之间没有区别。
初始化是必需的。它只能用于阅读目的。无法更改。到目前为止,“const”和“constexpr”关键字之间没有区别。
注意:我们可以在同一声明中使用constexpr和const。
constexpr const int* p;
Constexpr函数
通常,函数的返回值是在运行时获得的。但当满足某些条件时,对constexpr函数的调用将在编译时作为常量获得。
注意:在函数调用中发送给函数的参数变量的参数,如果有多个参数,则发送给所有参数变量,如果是C。E,则函数的返回值将在编译时计算!!!
constexpr int square (int a){
return a*a;
}
constexpr int a = 3;
constexpr int b = 5;
int arr[square(a*b+20)] = {0}; //This expression is equal to int arr[35] = {0};
为了使函数成为constexpr函数,函数的返回值类型和函数参数的类型必须在名为“literal type”的类型类别中。
constexpr函数是隐式内联函数。
重要一点:
无需使用常量表达式调用constexpr函数。它不是必需的。如果发生这种情况,计算将不会在编译时完成。它将被视为正常的函数调用。因此,当需要常量表达式时,我们将无法再使用此表达式。
constexpr函数所需的条件如下所示:;
1)函数参数中使用的类型和函数返回值的类型必须是文本类型。
2)函数内部不应使用具有静态寿命的局部变量。
3)如果函数是合法的,当我们在编译时使用常量表达式调用此函数时,编译器会在编译时计算函数的返回值。
4)编译器需要查看函数的代码,因此constexpr函数几乎总是在头文件中。
5)为了使我们创建的函数成为constexpr函数,函数的定义必须在头文件中。因此,无论哪个源文件包含该头文件,都将看到函数定义。
奖金
通常使用默认成员初始化,可以在类中初始化具有常量和整数类型的静态数据成员。然而,为了做到这一点,必须同时存在“常量”和“整型”。
如果我们使用静态constexpr,那么它不必是一个整型来在类中初始化它。只要用常量表达式初始化它,就没有问题。
class Myclass {
const static int sx = 15; // OK
constexpr static int sy = 15; // OK
const static double sd = 1.5; // ERROR
constexpr static double sd = 1.5; // OK
};
其他回答
const int var可以在运行时动态设置为一个值,一旦设置为该值,就不能再更改。
constexpr int变量不能在运行时动态设置,而是在编译时动态设置。一旦设置为该值,就不能再进行更改。
下面是一个可靠的例子:
int main(int argc, char*argv[]) {
const int p = argc;
// p = 69; // cannot change p because it is a const
// constexpr int q = argc; // cannot be, bcoz argc cannot be computed at compile time
constexpr int r = 2^3; // this works!
// r = 42; // same as const too, it cannot be changed
}
上面的代码段编译得很好,我已经注释掉了导致它出错的代码段。
这里需要注意的关键概念是编译时和运行时的概念。C++中引入了新的创新,旨在尽可能在编译时**了解**某些事情,以提高运行时的性能。
任何不涉及上述两个关键概念的解释都是幻觉。
const和constexpr都可以应用于变量和函数。尽管它们彼此相似,但实际上它们是非常不同的概念。
const和constexpr都意味着它们的值在初始化后不能更改。例如:
const int x1=10;
constexpr int x2=10;
x1=20; // ERROR. Variable 'x1' can't be changed.
x2=20; // ERROR. Variable 'x2' can't be changed.
const和constexpr之间的主要区别是它们的初始化值已知(求值)的时间。虽然const变量的值可以在编译时和运行时计算,但constexpr始终在编译时计算。例如:
int temp=rand(); // temp is generated by the the random generator at runtime.
const int x1=10; // OK - known at compile time.
const int x2=temp; // OK - known only at runtime.
constexpr int x3=10; // OK - known at compile time.
constexpr int x4=temp; // ERROR. Compiler can't figure out the value of 'temp' variable at compile time so `constexpr` can't be applied here.
了解该值在编译时或运行时是否已知的关键优势是,只要需要编译时常数,就可以使用编译时常数。例如,C++不允许您指定长度可变的C数组。
int temp=rand(); // temp is generated by the the random generator at runtime.
int array1[10]; // OK.
int array2[temp]; // ERROR.
这意味着:
const int size1=10; // OK - value known at compile time.
const int size2=temp; // OK - value known only at runtime.
constexpr int size3=10; // OK - value known at compile time.
int array3[size1]; // OK - size is known at compile time.
int array4[size2]; // ERROR - size is known only at runtime time.
int array5[size3]; // OK - size is known at compile time.
因此,常量变量可以定义编译时常量(如size1)和运行时常量(例如size2),前者可以用于指定数组大小,后者只能在运行时知道,不能用于定义数组大小。另一方面,constexpr总是定义可以指定数组大小的编译时常数。
const和constexpr也可以应用于函数。const函数必须是成员函数(方法、运算符),其中应用const关键字意味着该方法不能更改其成员(非静态)字段的值。例如
class test
{
int x;
void function1()
{
x=100; // OK.
}
void function2() const
{
x=100; // ERROR. The const methods can't change the values of object fields.
}
};
constexpr是一个不同的概念。它将函数(成员或非成员)标记为可以在编译时求值的函数,如果编译时常量作为其参数传递。例如,你可以写这个。
constexpr int func_constexpr(int X, int Y)
{
return(X*Y);
}
int func(int X, int Y)
{
return(X*Y);
}
int array1[func_constexpr(10,20)]; // OK - func_constexpr() can be evaluated at compile time.
int array2[func(10,20)]; // ERROR - func() is not a constexpr function.
int array3[func_constexpr(10,rand())]; // ERROR - even though func_constexpr() is the 'constexpr' function, the expression 'constexpr(10,rand())' can't be evaluated at compile time.
顺便说一句,constexpr函数是常规C++函数,即使传递了非常量参数,也可以调用这些函数。但在这种情况下,您得到的是非常量表达式值。
int value1=func_constexpr(10,rand()); // OK. value1 is non-constexpr value that is evaluated in runtime.
constexpr int value2=func_constexpr(10,rand()); // ERROR. value2 is constexpr and the expression func_constexpr(10,rand()) can't be evaluated at compile time.
constexpr还可以应用于成员函数(方法)、运算符甚至构造函数。例如。
class test2
{
static constexpr int function(int value)
{
return(value+1);
}
void f()
{
int x[function(10)];
}
};
一个更“疯狂”的样本。
class test3
{
public:
int value;
// constexpr const method - can't chanage the values of object fields and can be evaluated at compile time.
constexpr int getvalue() const
{
return(value);
}
constexpr test3(int Value)
: value(Value)
{
}
};
constexpr test3 x(100); // OK. Constructor is constexpr.
int array[x.getvalue()]; // OK. x.getvalue() is constexpr and can be evaluated at compile time.
概述
const保证程序不会更改对象的值。但是,const不能保证对象经历哪种类型的初始化。考虑:const int mx=numeric_limits<int>::max();//OK:运行时初始化函数max()只返回一个文字值。然而,由于初始值设定项是函数调用,mx会进行运行时初始化。因此,不能将其用作常量表达式:整数arr[mx];//错误:“需要常量表达式”constexpr是一个新的C++11关键字,它使您无需创建宏和硬编码文字。它还保证在某些条件下,对象进行静态初始化。它控制表达式的求值时间。通过强制对其表达式进行编译时求值,constexpr允许您在依赖编译时常量的任何代码中定义对时间关键型应用程序、系统编程、模板以及一般来说至关重要的真正常量表达式。
常量表达式函数
常量表达式函数是声明为constexpr的函数。它的主体必须是非虚拟的,除了typedef和静态断言之外,它只能由一个返回语句组成。其参数和返回值必须具有文本类型。它可以与非常量表达式参数一起使用,但当这样做时,结果不是常量表达式。
常量表达式函数旨在替换宏和硬编码文本,而不牺牲性能或类型安全性。
constexpr int max() { return INT_MAX; } // OK
constexpr long long_max() { return 2147483647; } // OK
constexpr bool get_val()
{
bool res = false;
return res;
} // error: body is not just a return statement
constexpr int square(int x)
{ return x * x; } // OK: compile-time evaluation only if x is a constant expression
const int res = square(5); // OK: compile-time evaluation of square(5)
int y = getval();
int n = square(y); // OK: runtime evaluation of square(y)
常量表达式对象
常量表达式对象是声明为constexpr的对象。必须使用常量表达式或由带有常量表达式参数的常量表达式构造函数构造的右值来初始化它。
常量表达式对象的行为就像声明为常量一样,除了它需要在使用之前进行初始化,并且其初始值设定项必须是常量表达式。因此,常量表达式对象始终可以用作另一个常量表达式的一部分。
struct S
{
constexpr int two(); // constant-expression function
private:
static constexpr int sz; // constant-expression object
};
constexpr int S::sz = 256;
enum DataPacket
{
Small = S::two(), // error: S::two() called before it was defined
Big = 1024
};
constexpr int S::two() { return sz*2; }
constexpr S s;
int arr[s.two()]; // OK: s.two() called after its definition
常量表达式构造函数
常量表达式构造函数是声明为constexpr的构造函数。它可以有一个成员初始化列表,但除了typedef和静态断言之外,它的主体必须为空。其参数必须具有文字类型。
常量表达式构造函数允许编译器在编译时初始化对象,前提是构造函数的参数都是常量表达式。
struct complex
{
// constant-expression constructor
constexpr complex(double r, double i) : re(r), im(i) { } // OK: empty body
// constant-expression functions
constexpr double real() { return re; }
constexpr double imag() { return im; }
private:
double re;
double im;
};
constexpr complex COMP(0.0, 1.0); // creates a literal complex
double x = 1.0;
constexpr complex cx1(x, 0); // error: x is not a constant expression
const complex cx2(x, 1); // OK: runtime initialization
constexpr double xx = COMP.real(); // OK: compile-time initialization
constexpr double imaglval = COMP.imag(); // OK: compile-time initialization
complex cx3(2, 4.6); // OK: runtime initialization
Scott Meyers的《有效的现代C++》一书中关于constexpr的提示:
constexpr对象是常量,并用编译期间已知的值初始化;constexpr函数在使用编译期间已知值的参数调用时产生编译时结果;constexpr对象和函数可以在比非constexpr的对象和函数更广泛的上下文中使用;constexpr是对象或函数接口的一部分。
资料来源:在C++中使用constexpr提高安全性、性能和封装。
首先,两者都是c++中的限定符。声明常量的变量必须初始化,以后不能更改。因此,通常声明为常量的变量甚至在编译之前都会有一个值。
但是,对于constexpr来说,它有点不同。
对于constexpr,您可以给出一个表达式,该表达式可以在程序编译期间计算。
显然,声明为constexper的变量不能像const一样在将来更改。
根据Bjarne Stroustrup的《C++编程语言第四版》一书•const:大致意思是“我保证不会改变这个值”(§7.5)。这主要用于指定接口,以便可以将数据传递给函数,而不必担心数据被修改。编译器执行const的承诺。•constexpr:大致意思是“在编译时要求值”(§10.4)。这主要用于指定常量,以允许例如:
const int dmv = 17; // dmv is a named constant
int var = 17; // var is not a constant
constexpr double max1 = 1.4*square(dmv); // OK if square(17) is a constant expression
constexpr double max2 = 1.4∗square(var); // error : var is not a constant expression
const double max3 = 1.4∗square(var); //OK, may be evaluated at run time
double sum(const vector<double>&); // sum will not modify its argument (§2.2.5)
vector<double> v {1.2, 3.4, 4.5}; // v is not a constant
const double s1 = sum(v); // OK: evaluated at run time
constexpr double s2 = sum(v); // error : sum(v) not constant expression
函数在常量表达式中可用,即在要计算的表达式中编译器必须将其定义为constexpr。例如:
constexpr double square(double x) { return x∗x; }
要成为constexpr,函数必须相当简单:只需要一个计算值的return语句。A.constexpr函数可以用于非常量参数,但当这样做时,结果不是常量表达式。我们允许使用非常量表达式参数调用constexpr函数在不需要常量表达式的上下文中,这样我们就不需要定义相同的函数两次:一次用于常量表达式,一次用于变量。在一些地方,语言规则需要常量表达式(例如,数组边界(§2.2.5,§7.3)、案例标签(§2.2.4、§9.4.2)、一些模板参数(§25.2)以及使用常量表达式)。在其他情况下,编译时评估对性能很重要。独立于性能问题,不变性(具有不可更改状态的对象)的概念是重要设计问题(§10.4)。