Hem constve constexprdeğişkenler ve fonksiyonlar uygulanabilir. Birbirlerine benzemelerine rağmen, aslında çok farklı kavramlardır.
Hem constve hem constexprbaşlangıç değerlerinden sonra değerlerinin değiştirilemeyeceği anlamına gelir. Yani mesela:
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.
Arasındaki temel fark, constve constexprbunların başlatma değerleri bilinen zamanı (değerlendirildi). constDeğişkenlerin değerleri hem derleme zamanında hem de çalışma zamanında constexprdeğerlendirilebilirken , her zaman derleme zamanında değerlendirilir. Örneğin:
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.
Değerin derleme zamanında veya çalışma zamanında bilinip bilinmediğini bilmenin en önemli avantajı, derleme süresi sabitlerinin her zaman derleme sabitine ihtiyaç duyulduğunda kullanılabilmesidir. Örneğin, C ++ değişken uzunluklu C dizilerini belirtmenize izin vermez.
int temp=rand(); // temp is generated by the the random generator at runtime.
int array1[10]; // OK.
int array2[temp]; // ERROR.
Yani şu anlama gelir:
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.
Yani constdeğişkenler tanımlayabilirsiniz hem derleme zamanı sabitleri gibi size1o dizi boyutları ve belirtmek için kullanılabilir çalışma zamanı sabitleri gibi size2sadece çalışma zamanında bilinir ve dizi boyutlarını tanımlamak için kullanılamaz. Öte yandan constexprher zaman dizi boyutlarını belirleyebilen derleme zamanı sabitlerini tanımlayın.
Her ikisi de constve constexprişlevlere de uygulanabilir. Bir constişlev, constanahtar sözcük uygulamasının , yöntemin üye (statik olmayan) alanlarının değerlerini değiştiremeyeceği anlamına geldiği bir üye işlevi (yöntem, operatör) olmalıdır . Örneğin.
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.
}
};
A constexprfarklı bir kavramdır. Bir işlevi (üye veya üye olmayan), derleme zamanı sabitleri bağımsız değişkenleri olarak iletilirse derleme zamanında değerlendirilebilen işlev olarak işaretler . Örneğin bunu yazabilirsiniz.
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.
Bu arada constexprişlevler, sabit olmayan argümanlar iletilse bile çağrılabilen normal C ++ işlevleridir. Ancak bu durumda sınırsız değerleri elde edersiniz.
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.
constexprAyrıca üye işlevlerini (yöntem), operatörler ve hatta kurucular uygulanabilir. Örneğin.
class test2
{
static constexpr int function(int value)
{
return(value+1);
}
void f()
{
int x[function(10)];
}
};
Daha 'çılgın' bir örnek.
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.
constexprderleme zamanı sabiti oluşturur;constbasitçe, değerin değiştirilemeyeceği anlamına gelir.