Date: Sat, 4 Apr 2026 10:20:54 +0500
How does the compiler know what's gonna happen at runtime?
The compiler will of course not no a lot on what's gonna happen at runtime,
but it can branch or do whatever it sees fit to generate code to find out
what gonna happen(the index) at time using the techniques explained below:
The compiler knows about the next line because of three things, two are c++
concepts and the other is a compiler concept:
1.*********compiler concept*********:
*********Abstract syntax tree*********:
The abstract syntax tree is as explained by:
(Credits to researchgate):
https://www.researchgate.net/figure/An-example-abstract-tree-built-by-Cp-3_fig1_224251545
Every compilation tool converted there programs to a abstract syntax tree
in concept, and then optimizations take place to optimize the
representation that they compile that program into. This construct would
make the intermediate representation(llvm representation(the representation
before assembly))optimal.
This is also a core concept (the first thing that you learn) in every
compiler design class.
*********Basically*********:
in this case the c++ has context of where the object is defined and where
it's indexed, the AST representation can be looked up before the code
reaches to intermediate representation and be optimize at that level if
that's what the compiler thinks is the best.
*********Basically*********
Extended discussion on this one point:
2.*********context and intent(c++)*********:
*********Context*********:
Context in cpp in the case of templates is the context of defintion and the
context of instantion. In this case, the logic for the two terms are the
same for the compiler.
*********Context of defintion*********:
In the case of the new construct, the context of defintion gives you the
when, where, and how book keeping(to optimize the runtime indexing) could
be at a intermediate level, and could be optimized *********while
keeping*********:
it's sorounding context(for example ,the probable function stack
representation at that point) and the programs whole abstract tree in mind.
*********Context of instantiation*********:
Context of instigation is when the template is actually expanded into a
function or a class. This instantion can be done at very low level phases
of compilation hence the compiler might delay or not delay it based on
what's best. Infact bjarne Stroustrup in his book (c++ programming 4th
edition section 23.2.2) that inline keyword and templates work very world
together because for each different instantiation, the compiler has
flexibility to weather inline or to not inline(,and I would also
add:weather it would backfire or not).
Really sorry for the confusion, and about that:
Please Don't mind a short answer on const: it's because you asked to
provide the basic semantics and I think const provides some type safe
fence, but I can agree that it's bad idea (ugly because of const cast).
Thanks for your feedback!
Regards, Muneem
On Sat, 4 Apr 2026, 9:39 am Andre Kostur, <andre_at_[hidden]> wrote:
> On Sat, Apr 4, 2026 at 4:31 AM Muneem <itfllow123_at_[hidden]> wrote:
> >
> > Hi!
> > Mr.Andre kustor asked a very good and welcomed question:
> > If I were to write:
> >
> > auto & x =
> return_index_chosen(read_from_string<uint8_t>(string_to_read_from,
> > pos));
> >
> > What is the type of x ? Particularly if string_to_read_from is a runtime
> value.
> >
> > The answer is the *********const reference type********* of the object
> at the index read by "read_from_string<uint8_t>(string_to_read_from,
> > pos)" that of course can be modified after type casting const_cast. if
> the "const" is an issue then please propose something else, I really need
> to see if it's not perfect.
>
> Why is const suddenly part of the discussion? Actually, don't answer
> that: there's more important things to worry about before we need to
> talk about that.
>
> How does the compiler know if in the next line I may do
> "x.capacity();"? Remember, read_from_string is getting me a runtime
> value. How does the compiler know whether to accept my call to
> capacity()?
>
The compiler will of course not no a lot on what's gonna happen at runtime,
but it can branch or do whatever it sees fit to generate code to find out
what gonna happen(the index) at time using the techniques explained below:
The compiler knows about the next line because of three things, two are c++
concepts and the other is a compiler concept:
1.*********compiler concept*********:
*********Abstract syntax tree*********:
The abstract syntax tree is as explained by:
(Credits to researchgate):
https://www.researchgate.net/figure/An-example-abstract-tree-built-by-Cp-3_fig1_224251545
Every compilation tool converted there programs to a abstract syntax tree
in concept, and then optimizations take place to optimize the
representation that they compile that program into. This construct would
make the intermediate representation(llvm representation(the representation
before assembly))optimal.
This is also a core concept (the first thing that you learn) in every
compiler design class.
*********Basically*********:
in this case the c++ has context of where the object is defined and where
it's indexed, the AST representation can be looked up before the code
reaches to intermediate representation and be optimize at that level if
that's what the compiler thinks is the best.
*********Basically*********
Extended discussion on this one point:
2.*********context and intent(c++)*********:
*********Context*********:
Context in cpp in the case of templates is the context of defintion and the
context of instantion. In this case, the logic for the two terms are the
same for the compiler.
*********Context of defintion*********:
In the case of the new construct, the context of defintion gives you the
when, where, and how book keeping(to optimize the runtime indexing) could
be at a intermediate level, and could be optimized *********while
keeping*********:
it's sorounding context(for example ,the probable function stack
representation at that point) and the programs whole abstract tree in mind.
*********Context of instantiation*********:
Context of instigation is when the template is actually expanded into a
function or a class. This instantion can be done at very low level phases
of compilation hence the compiler might delay or not delay it based on
what's best. Infact bjarne Stroustrup in his book (c++ programming 4th
edition section 23.2.2) that inline keyword and templates work very world
together because for each different instantiation, the compiler has
flexibility to weather inline or to not inline(,and I would also
add:weather it would backfire or not).
Really sorry for the confusion, and about that:
Please Don't mind a short answer on const: it's because you asked to
provide the basic semantics and I think const provides some type safe
fence, but I can agree that it's bad idea (ugly because of const cast).
Thanks for your feedback!
Regards, Muneem
On Sat, 4 Apr 2026, 9:39 am Andre Kostur, <andre_at_[hidden]> wrote:
> On Sat, Apr 4, 2026 at 4:31 AM Muneem <itfllow123_at_[hidden]> wrote:
> >
> > Hi!
> > Mr.Andre kustor asked a very good and welcomed question:
> > If I were to write:
> >
> > auto & x =
> return_index_chosen(read_from_string<uint8_t>(string_to_read_from,
> > pos));
> >
> > What is the type of x ? Particularly if string_to_read_from is a runtime
> value.
> >
> > The answer is the *********const reference type********* of the object
> at the index read by "read_from_string<uint8_t>(string_to_read_from,
> > pos)" that of course can be modified after type casting const_cast. if
> the "const" is an issue then please propose something else, I really need
> to see if it's not perfect.
>
> Why is const suddenly part of the discussion? Actually, don't answer
> that: there's more important things to worry about before we need to
> talk about that.
>
> How does the compiler know if in the next line I may do
> "x.capacity();"? Remember, read_from_string is getting me a runtime
> value. How does the compiler know whether to accept my call to
> capacity()?
>
Received on 2026-04-04 05:21:12
