如何提升网站营销力,鹤山做网站,关于门户网站建设的整改报告,广州微网站制作constinit说明符断言(assert)变量具有静态初始化#xff0c;即零初始化和常量初始化(zero initialization and constant initialization)#xff0c;否则程序格式不正确(program is ill-formed)。 constinit说明符声明具有静态或线程存储持续时间(thread storage duration)的… constinit说明符断言(assert)变量具有静态初始化即零初始化和常量初始化(zero initialization and constant initialization)否则程序格式不正确(program is ill-formed)。 constinit说明符声明具有静态或线程存储持续时间(thread storage duration)的变量。如果使用constinit声明变量则必须使用constinit应用其初始化声明。如果使用constinit声明的变量具有动态初始化(即使以静态初始化执行)则程序格式不正确。 constinit不能与constexpr一起使用。当声明的变量是引用时constinit等同于constexpr。当声明的变量是对象时constexpr要求该对象必须具有静态初始化和常量析构并使该对象带有const限定的(const-qualified)但是constexpr不要求常量析构和带有const限定。因此具有constexpr构造函数但没有constexpr析构函数的类型的对象可能用constinit声明但不用constexpr声明。 constinit还可以用于非初始化声明中以告知编译器thread_local变量已初始化从而减少隐藏保护变量可能产生的开销。thread_local变量对于每个线程都有一个单独的实例并且它们的值在线程的整个生命周期内保留。 constinit允许变量在编译时或动态初始化时初始化但不能在运行时初始化。它确保变量只初始化一次并且初始化后其值不能更改。 constinit用于声明静态和thread_local变量这些变量保证使用常量表达式进行初始化。 具有静态存储持续时间(static storage duration)的变量在程序启动时初始化一次并在程序的整个生命周期内保留其值。 constinit用于指示变量应在编译时初始化。该变量必须声明为constexpr并且只能使用常量表达式初始化使其成为编译时常量。 constinit解决了初始化顺序问题(确保以受控方式初始化变量从而减少由于未初始化和部分初始化的变量而导致意外行为的可能性)提高了静态对象初始化的稳健性。 constinit可防止在运行时初始化具有静态存储持续时间的变量。使用constinit说明符指定的变量需要使用常量表达式进行初始化。 constinit不能与constexpr或consteval一起使用因为constinit用于变量的静态初始化这发生在程序开始执行之前而constexpr和consteval用于在编译时评估表达式。 初始化具有静态存储持续时间的变量可能会导致两种结果变量在编译时初始化(常量初始化)变量在控制第一次通过其声明时初始化。 constinit只能应用于具有静态存储持续时间的变量。如果修饰变量在编译时未初始化则程序格式不正确(即无法编译)。使用constinit可确保变量在编译时初始化并且不会发生静态初始化顺序混乱。 constexpr确实意味着constinit。反之则不是。 变量可以同时是const和constinit。它不能同时是constexpr和constinit。 constinit只能应用于具有静态或线程存储持续时间的变量。将它应用于其他变量是没有意义的因为constinit都是关于静态初始化的。 以下为测试代码
namespace {
template typename T
class LuckyNum {
public:inline static constinit int num{ 66 };//inline static int num{ 66 }; // ok//inline static constexpr int num{ 66 }; // ok
};consteval int factorial(int n) {
//constinit consteval int factorial(int n) { // error C2216: constinit不能和consteval一起使用return n 0 ? 1 : n * factorial(n - 1);
}constinit int arr1[] { 1, 2, 3, factorial(4) };
constexpr int arr2[] { 1, 2, 3, factorial(4) };
//constinit constexpr int arr3[] { 1, 2, 3, factorial(4) }; // error C2216: constinit不能和constexpr一起使用
const constinit int arr4[] { 1, 2, 3, factorial(4) };const char* gg() { return dynamic initialization; }
constexpr const char* ff(bool p) { return p ? constant initializer : gg(); }constinit const char* c ff(true); // OK
//constinit const char* d ff(false); // ERROR: gg is not constexpr, so d cannot be evaluated at compile-time} // namespaceint test_constinit()
{LuckyNumint num1;LuckyNumstd::string num2;std::cout num1 value: num1.num std::endl;std::cout num2 value: num2.num std::endl;std::cout arr1[3]: arr1[3] std::endl;std::cout arr2[3]: arr2[3] std::endl;return 0;
} 执行结果如下图所示 GitHubhttps://github.com/fengbingchun/Messy_Test 文章转载自: http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.xzgbj.cn.gov.cn.xzgbj.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.lgnbr.cn.gov.cn.lgnbr.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.nkllb.cn.gov.cn.nkllb.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.hxftm.cn.gov.cn.hxftm.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.gychx.cn.gov.cn.gychx.cn