[PDF] [PDF] Михаил Матросов

non-volatile non-template non-inline const-qualified variables (including constexpr) that aren't declared extern and aren't previously declared to have external Properties of entities with static and thread storage duration in C++17 # Property local const std::string s4 = "4"; // If not a literal type; implicitly static // Anywhere



Previous PDF Next PDF





[PDF] Constexpr Lambda - Standard C++

28 avr 2015 · literal type if the type of each of its data-members is a literal type; In C++14, a lambda-expression is prohibited from appearing within a are verboten inside constant expressions; either because those functions can not be specified I'm confident we'll have a good answer for lambdas + constexpr in 



[PDF] constexpr Introduction - Scott Schurr - CppCon 2015key

constexpr objects can't change at runtime Constant 9 • May be any literal type including: • Floating point types • Character literals • Pointer calculations might not have the same error: constexpr variable 'mask' must be initialized by a



[PDF] Generalized Constant Expressions - Bjarne Stroustrups Homepage

26 fév 2006 · the proposal for Literals for user-defined types [Str03], Generalized initializer In current C++, a variable or static data member declared const can be used in an integral A constant-expression function cannot be called before it is As for other const variables, storage need not be allocated for a constant-



[PDF] Permitting trivial default initialization in constexpr contexts - Open-std

15 juil 2019 · default initialization does not compile in constexpr As of C++17, this fails to compile meaning `Example1::f(const T&)` is not the need for this proposed change As always, undefined behavior cannot be invoked in constexpr (3 4 4) - a definition of a variable of non-literal type or of static or thread 



[PDF] Exploring constexpr at Runtime - Open-std

13 mar 2013 · 9 1 How Does constexpr Participate in Overloading in C++11 Today? While declaring each variable constexpr is not onerous, it is error prone It's very easy to forget object shall have literal type and shall be initialized constexpr cannot be used to reliably perform such error checks during compilation



[PDF] Extern template constexpr - Squarespace

Today I have Daniele Parastrelli's guest post about extern templates for you It was thought to start in (C++20 or later) If the constexpr variable is not the following requirements: the return type of c++20 (up to C++20) must be literal in its return Destructors cannot be constexpr, but constant expressions can implicitly call 



[PDF] Михаил Матросов

non-volatile non-template non-inline const-qualified variables (including constexpr) that aren't declared extern and aren't previously declared to have external Properties of entities with static and thread storage duration in C++17 # Property local const std::string s4 = "4"; // If not a literal type; implicitly static // Anywhere



[PDF] z/OS XL C/C++ Language Reference - IBM

14 déc 2020 · The decltype(expression) type specifier (C++11) The constexpr specifier The aligned variable attribute If you do not have the Adobe Reader, you can download it You cannot use a name with no linkage to declare an entity with Adjacent identifiers, keywords, and literals must be separated with 

[PDF] c++ event driven programming

[PDF] c++ fopen exclusive

[PDF] c++ lambda

[PDF] c++ lambda as parameter

[PDF] c++ math functions

[PDF] c++ scientific computing library

[PDF] c++ template polymorphism

[PDF] c++ to ada

[PDF] c1v1 = c2v2 = c3v3

[PDF] c1v1=c2v2 calculator

[PDF] c1v1=c2v2 dna

[PDF] c1v1=c2v2 excel

[PDF] c1v1=c2v2 khan academy

[PDF] c1v1=c2v2 percentage calculator

[PDF] c1v1=c2v2 titration

Спецификаторы, квалификаторы и шаблоныМихаил МатросовAlign Technology

2

Building a C/C++ program3a.cpp#include "a.hpp"#include "b.hpp"a.hppb.hppb.cpp#include "a.hpp"#include "b.hpp"a.ia.hppb.hppa.cppb.ia.hppb.hppb.cpppreprocesstranslation unitssource filesa.obj/.ob.obj/.ocompileobject filesresult.lib/.dll/.exe/.a/.so/.outlinklibrary / executable

Linkage5intsqr(intx){returnx*x;}intsqr(intx);boolcheck(inta,intb,intc){returnsqr(a)+sqr(b)==sqr(c);}a.cppb.cppexternal linkage⇒link time error: sqrnot found in b.objstaticintsqr(intx){returnx*x;}a'.cppinternal linkage

Linkage6can be referred to fromexternal linkageAll translation unitsinternal linkageCurrent translation unitno linkageCurrent scope

Storage duration7boolbeetlejuice(){intcounter=0;return++counter>=3;}automatic storage durationboolbeetlejuice(){static intcounter=0;return++counter>=3;}static storage duration

Storage duration•Applicable only to objects•Time of initialization/destruction is more complicated•Storage class specifiers: static, extern, thread_local, mutable8storage allocated/deallocatedautomaticWhen entering/leaving the scope or when creating/destroying an object of a classstaticWhen the program begins/endsthreadWhen the thread begins/endsdynamicWhen new/deleteis called

10

Any of the following names declared at namespace scope have internal linkage:non-volatile non-template non-inline const-qualified variables (including constexpr) that aren't declared extern and aren't previously declared to have external linkage;11constdoublethickness=0.65;constchar*name="tooth";common.hpp#include"common.hpp"a.cpp#include"common.hpp"b.cpp⇒link time error: multiple definitions of symbol "name"internal linkageexternal linkage

Any of the following names declared at namespace scope have internal linkage:non-volatile non-template non-inline const-qualified variables (including constexpr) that aren't declared extern and aren't previously declared to have external linkage;12constexprdoublethickness=0.65;conststd::stringname="tooth";common.hpp#include"common.hpp"a.cpp#include"common.hpp"b.cppAconstexprspecifier used in an object declarationimpliesconst.internal linkageinternal linkage

13#include"common.hpp"#includebooldumbCmp(constchar*s1,constchar*s2){returns1==s2;}intmain(){std::cout<

14inline conststd::stringname="tooth";constchar*getName();#include"common.hpp"#includebooldumbCmp(constchar*s1,constchar*s2){returns1==s2;}intmain(){std::cout<

15intsqr(intx){returnx*x;}common.hpp#include"common.hpp"a.cpp#include"common.hpp"b.cppexternal linkage⇒link time error: multiple definitions of symbol "sqr"

16constexprintsqr(intx){returnx*x;}common.hpp#include"common.hpp"a.cpp#include"common.hpp"b.cppexternal (weak) linkageA constexprspecifier used in a functionor static member variable declaration implies inline.

17voidother();structLocal{staticvoidfoo(){std::cout<<"main ";}};intmain(){Local::foo();other();}main.cppexternallinkagestructLocal{staticvoidfoo(){std::cout<<"other ";}};voidother(){Local::foo();}other.cpp⇒main mainexternallinkageEvery program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement (9.4.1); no diagnostic required.

18voidother();namespace {structLocal{staticvoidfoo(){std::cout<<"main ";}};}intmain(){Local::foo();other();}main.cppinternallinkagenamespace {structLocal{staticvoidfoo(){std::cout<<"other ";}};}voidother(){Local::foo();}other.cpp⇒main otherinternallinkageIn addition, all names declared in unnamed namespace or a namespace within an unnamed namespace, even ones explicitly declared extern, have internal linkage.

Собираем в кучу19

Цвета имеют значение•statickeyword•static storage duration•internal linkage20

Допустимые комбинацииstorage duration и linkage21Storage durationno linkageinternal linkageexternal linkageautomaticlocal variablestaticstaticlocal variablestaticglobal variableglobal variablethreadthread_locallocal variablethread_localstatic global variablethread_localglobal variabledynamicLinkage

How to identifystorage duration of an object?22scope?staticnamespace (global)block (local) or class (member)yesnostatic?automaticthread_local?threadyesno

Properties of entities with static and thread storage duration in C++17#Propertylocalvariableglobal variablemember variableglobal functionmember function1no linkage2external linkage3constexpr⇒const4constexpr⇒inline5const⇒* internal linkageN/A6inline⇒external (weak) linkageN/A7static ⇒internal linkageRequiredRequiredRequired8anonymous (unnamed) namespace⇒internal linkage23* if not volatileand not templateApply in orderEntity

24

26#include"common.hpp"#includebooldumbCmp(constchar*s1,constchar*s2){returns1==s2;}intmain(){std::cout<

27#include"common.hpp"#includeconststd::stringname="tooth";booldumbCmp(constchar*s1,constchar*s2){returns1==s2;}intmain(){std::cout<

Properties of entities with static and thread storage duration in C++17#Propertylocalvariableglobal variablemember variableglobal functionmember function1no linkage2external linkage3constexpr⇒const4constexpr⇒inline5const⇒* internal linkageN/A6inline⇒external (weak) linkageN/A7static ⇒internal linkageRequiredRequiredRequired8extern ⇒external linkage (declaration)N/AN/ARedundantN/A9anonymous (unnamed) namespace⇒internal linkage29* if not volatile,not template, and not previously declared externApply in orderEntity

Practice time!30

Properties of entities with static and thread storage duration in C++17#Propertylocalvariableglobal variablemember variableglobal functionmember function1no linkage2external linkage3constexpr⇒const4constexpr⇒inline5const⇒* internal linkageN/A6inline⇒external (weak) linkageN/A7static ⇒internal linkageRequiredRequiredRequired8extern ⇒external linkage (declaration)N/AN/ARedundantN/A9anonymous (unnamed) namespace⇒internal linkage32* if not volatile,not template, and not previously declared externApply in orderEntity

34compile timerun time* when used to modify storage duration

Шаблоны36

38
39

inline никогда не подведёт40templateinlineconstexprboolis_const_v=is_const::value;

Long road to const43

44// header.hpp#definen42//Cannotcontainexpressions;simplyevilconstintn=42;//Duplicatedineachtranslationunitexternconstintn;//Initializerisnotvisible; need manual definitioninlineintn(){//Needsparenstobeused;returnsrvaluereturn42;}enum{n=42//Worksonlyforintegers};How to declare a constant before C++17?

45//header.hppinlineconstexprintn1=1;//Defaultchoiceinlineconststd::strings2="2";//Ifnotaliteraltype//source.cppormodule.ixxconstexprintn3=3;//Defaultchoice;implicitlystaticconststd::strings4="4";//Ifnotaliteraltype;implicitlystatic//AnywherestructA{staticconstexprintn=5;//Defaultchoice;implicitlyinlinestaticinlineconststd::strings="6";//Ifnotaliteraltype};voidf(){staticconstexprintn=7;//Defaultchoicestaticconststd::strings="8";//Ifnotaliteraltype}How to declare a constant with an initializer?

46//headers are no longer used//whatever possible is constexpr//module.ixxconstexprintn3=3;//AnywherestructA{staticconstexprintn=5;};voidf(){staticconstexprintn=7;}How to declare a constant with an initializer in an ideal world?

How to declare a constant with an initializer?47global?+staticyesnoliteral?global && header?constmember?+constexpr+const+inline+type name = init;noyesnoyesyesno1.If not global add static2.If possible add constexpr, otherwise add const3.If global in a header, or constmember, add inline

Пример48

templatestaticinlinethread_localconstexprconstvolatileTx={};Implied by constexpr templatestaticinlinethread_localconstexprvolatileTx={};Overridden by static

constexprvolatile variable template with thread storage duration and internal linkagetemplatestaticthread_localconstexprvolatileTx={};thread storage durationinternal linkage

C++2052

Modules53can be referred to fromexternal linkageAll translation unitsinternal linkageCurrent translation unitno linkageCurrent scopecan be referred to fromexternal linkageAll modulesmodule linkageAll translation units within this moduleinternal linkageCurrent translation unitno linkageCurrent scopeC++17C++20

58

Properties of entities with static and thread storage duration in C++20**#Propertylocalvariableglobal variablemember variableglobal functionmember function1no linkage2external linkage3consteval⇒constexprN/AN/AN/A4constexpr⇒const&& constinit5constexpr⇒inline6const⇒* internal linkageN/A7inline⇒external (weak) linkageN/A8static ⇒internal linkageRequiredRequiredRequired9extern ⇒external linkage (declaration)N/AN/ARedundantN/A10anonymous (unnamed) namespace⇒internal linkage59* if not volatile,not template, and not previously declared externApply in orderEntity** ignoring modules

constexprconst60 constexprconst61constexprconstchar*foo() {return"cppconf";}

constexprconst62structParams{doubleparam1;doubleparam2;};constexprParamsGlobalParamsWithDescriptiveName={3.14,2.72};voidfoo(){constexprParams¶ms=GlobalParamsWithDescriptiveName;}⇒error: binding reference of type 'Params&' to 'constParams' discards qualifiers

constexprreferences64Paramsp;//Value;constParams*pcp=&p;//PointertoconstParams*constcpp=&p;//ConstpointerconstParams&rcp=p;//ReferencetoconstParams&constcrp=p;//Constreference(doesnotcompile)static_assert(!std::is_const_v);voidfoo(){constexprconstParams¶ms=GlobalParamsWithDescriptiveName;}Applies to objectApplies to reference itself

68

Подвал69

constexprtrap•constexprimpliesinline for a function•constexprfunctions with same signature and different bodies might interfere!-Note that return type is not part of a mangled name!-Different behavior depending on optimization!•If the functions weren't inline, the linker would warn you70

Random•An inline functionor variablewithexternal linkage must be declaredinlinein every translation unit-However, works for gcc/clang (clang -c; nm, weak symbol)-Indeed does not work for VC++ (dumpbin/symbols)74

Things to clarify•https://github.com/ericniebler/range-v3/blob/9930c89e005e203ff8294f461e5ac99277d3b088/include/range/v3/iterator/diffmax_t.hpp#L430-why need inline definitions for integral static members?•Does inline slow down linker?•linkage of template instantiations•inline and extern, https://stackoverflow.com/a/14017155/26121775

Переводы•implies -влечёт•linking -компоновка76

quotesdbs_dbs22.pdfusesText_28