Topic: Compile-time static `std::integral_counter`
Author: Vittorio Romeo <vittorio.romeo.vee@gmail.com>
Date: Fri, 19 Sep 2014 06:42:47 -0700 (PDT)
Raw View
------=_Part_117_608700932.1411134167813
Content-Type: text/plain; charset=UTF-8
*Introduction*
There have been many occasions in my games/libraries where I need to assign
a *`0..n` *compile-time integer to a series of types. Here are two examples:
- In one of my component-based entity management systems, I assign an
*`std::bitset`* to every entity, where every bit corresponds to a
specific component type.
- One of my polymorphic memory management/recycling classes stores
several "chunks" of allocated memory for specific types that are part of
the same hierarchy.
To quickly retrieve the chunks, they're stored in a contiguous array.
Every index of the array corresponds to a chunk of a specific type.
The problem here is assigning an unique integer, starting from zero, to a
specific type, and maintaining it throughout the whole program. This
requires keeping track of the last used integer.
*Current solution/workaround*
Sacrificing performance, it is currently possible to achieve what I
described above. Not at compile-time, but at run-time instead.
// Statically store the last used global unique id.
inline int getLastId() noexcept
{
static int lastId{0};
return lastId++;
}
// Statically store an unique id for a specific type `T`.
template<typename T> int getIdForType() noexcept
{
static int id{getLastId()};
return id;
}
struct TypeA { };
struct TypeB { };
struct TypeC { };
int main()
{
// Whenever `getIdForType<T>()` gets instantiated,
// an unique id is "bound" to `T` for the rest of the program.
std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
return 0;
}
The workaround allows me to implement the aforementioned examples. However,
it requires *`static`* storage for something that could be determined at
compile-time.
It also requires overhead to access this storage every time we need to get
a type's unique id.
*Possible proposal*
Implement a stateful compile-time *`std::integral_counter` *class that
somehow *(compiler magic?) *remembers its value.
// We need to "store" a counter during compile-time.
// A new keyword/contextual keyword combination is probably required for
this.
// We basically "allocate" memory for this counter during compilation and
// use the counter during compilation. The counter should not exist anymore
// at run-time (or be accessible at run-time).
static template std::integral_counter<int, 0> lastIdCounter;
// ____________ _____________________ ___ _ _____________
// | | | | L name
// L contextual keyword combination | |
// | | L counter starting value
// L type L counter value type
// Everything is done at compile-time. Constexpr should work, but also using
// `std::integral_constant` with a template struct.
template<typename T> constexpr int getIdForType() noexcept
{
constexpr int id{lastIdCounter::value}; // Get the compile-time
counter's current value
lastIdCounter::increment(); // Increment the compile-time counter - it
will remember its last value
return id;
}
int main()
{
// Whenever `getIdForType<T>()` gets called with a certain `T`, a value
from
// the compile-time counter is "bound" to that `T` for the rest of the
program.
// Since it is `constexpr` and evaluated at compile-time, there's no
need to store
// the id anywhere in memory - calling the `getIdForType<T>()` function
can
// basically be thought of as replacing `getIdForType<T>()` with a
constant
// at compile time.
std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
return 0;
}
Thoughts?
What motivated me to write this post is the fact that we have all necessary
information at compile-time to assign an unique constant id to a specific
type - but there (apparently) is no way of doing so.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_117_608700932.1411134167813
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><font size=3D"4"><b>Introduction</b></font><div><font size=
=3D"2">There have been many occasions in my games/libraries where I need to=
assign a </font><b style=3D"font-size: small;">`0..n` </b><font size=3D"2"=
>compile-time integer to a series of types. Here are two examples:</font><b=
r><ul style=3D"font-size: small;"><li><span style=3D"line-height: normal;">=
In one of my component-based entity management systems, I assign an <b>`std=
::bitset`</b> to every entity, where every bit corresponds to a specif=
ic component type.</span></li><li><span style=3D"line-height: normal;">One =
of my polymorphic memory management/recycling classes stores several "chunk=
s" of allocated memory for specific types that are part of the same hierarc=
hy. <br>To quickly retrieve the chunks, they're stored in a contiguous arra=
y. Every index of the array corresponds to a chunk of a specific type.</spa=
n></li></ul><div style=3D"font-size: small;">The problem here is assigning =
an unique integer, starting from zero, to a specific type, and maintaining =
it throughout the whole program. This requires keeping track of the last us=
ed integer.</div><div style=3D"font-size: small;"><br></div><div style=3D"f=
ont-size: small;"><br></div><div style=3D"font-size: small;"><br></div><div=
style=3D"font-size: small;"><b style=3D"font-size: large;">Current solutio=
n/workaround</b><br></div><div style=3D"font-size: small;">Sacrificing perf=
ormance, it is currently possible to achieve what I described above. Not at=
compile-time, but at run-time instead.</div><div style=3D"font-size: small=
;"><br></div><div style=3D"font-size: small;"><div class=3D"prettyprint" st=
yle=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgro=
und-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"s=
ubprettyprint"><span style=3D"color: #800;" class=3D"styled-by-prettify">//=
Statically store the last used global unique id.</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">inline</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> getLastId</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> noexcept<br></span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br> </span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">static</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> lastId</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span sty=
le=3D"color: #066;" class=3D"styled-by-prettify">0</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">};</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br> </span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">return</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> lastId</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">++;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br><br></span><span style=3D"color: #800;" class=3D"styled-by-pr=
ettify">// Statically store an unique id for a specific type `T`.</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">template</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">typename</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">></span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> getIdForType</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> noexcept<br></span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r> </span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">static</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> id</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">getLastId</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">()};</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br> </span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> id</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled=
-by-prettify">struct</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify"=
>TypeA</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">};</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;"=
class=3D"styled-by-prettify">struct</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"sty=
led-by-prettify">TypeB</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><font color=3D"#666600"><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br></span></font><span style=3D"color: #008;" class=3D"styled-by-prettify=
">struct</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #606;" class=3D"styled-by-prettify">TypeC</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><font co=
lor=3D"#666600"><span style=3D"color: #660;" class=3D"styled-by-prettify">{=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">};</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br></span></font><font col=
or=3D"#666600"><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> main</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">()</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br> </span><span style=3D"col=
or: #800;" class=3D"styled-by-prettify">// Whenever `getIdForType<T>(=
)` gets instantiated, </span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br> </span><span style=3D"color: #800;" class=3D"=
styled-by-prettify">// an unique id is "bound" to `T` for the rest of the p=
rogram.</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
><br> std</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">cout </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
lt;<</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> ge=
tIdForType</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
<</span><span style=3D"color: #606;" class=3D"styled-by-prettify">TypeA<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">>()</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify"><<</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">endl</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by=
-prettify">// Prints '0'</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"><br></span></font><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> std</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">cout </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify"><<</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> getIdForType</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify"><</span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">TypeB</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>>()</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify"><<</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">endl</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #800;" class=
=3D"styled-by-prettify">// Prints '1'</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br> std</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify"><<</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> getIdForType</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify"><</span><span style=3D"color: #606;" class=3D=
"styled-by-prettify">TypeC</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">>()</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y"><<</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">endl</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #800;" class=3D"styled-by-prettify">// Prints '2'</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br></span><font color=3D"#666600"=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>  =
; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #066;" class=3D"styled-by-prettify">0</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">}</span></font></div></code></div><br>The w=
orkaround allows me to implement the aforementioned examples. However, it r=
equires <b>`static`</b> storage for something that could be determined at c=
ompile-time. </div><div style=3D"font-size: small;">It also requires o=
verhead to access this storage every time we need to get a type's unique id=
..</div><div style=3D"font-size: small;"><br></div><div style=3D"font-size: =
small;"><br></div><div style=3D"font-size: small;"><br></div><div><font siz=
e=3D"4"><b>Possible proposal</b></font></div><div>Implement a stateful comp=
ile-time <b>`std::integral_counter` </b>class that somehow <i>(compile=
r magic?) </i>remembers its value.</div><div><br></div><div><div class=3D"p=
rettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break=
-word; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><=
div class=3D"subprettyprint"><span style=3D"color: #800;" class=3D"styled-b=
y-prettify">// We need to "store" a counter during compile-time.</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span styl=
e=3D"color: #800;" class=3D"styled-by-prettify">// A new keyword/contextual=
keyword combination is probably required for this.</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"colo=
r: #800;" class=3D"styled-by-prettify">// We basically "allocate" memory fo=
r this counter during compilation and</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br></span><span style=3D"color: #800;" class=3D=
"styled-by-prettify">// use the counter during compilation. The counter sho=
uld not exist anymore</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br></span><span style=3D"color: #800;" class=3D"styled-by-prett=
ify">// at run-time (or be accessible at run-time).</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">static</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">template</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">integral_counter</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify"><</span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">int</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #066;" class=3D"styled-by-prettify">0</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">></span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> lastIdCounter</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: =
#800;" class=3D"styled-by-prettify">// ____________ _____________________ _=
__ _ _____________</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br></span><span style=3D"color: #800;" class=3D"styled=
-by-prettify">// | | =
| | &=
nbsp;L name</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br></span><span style=3D"color: #800;" class=3D"styled-by-prettify">// L =
contextual keyword combination | |</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color=
: #800;" class=3D"styled-by-prettify">// =
| &nb=
sp; | L counter starting value</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #8=
00;" class=3D"styled-by-prettify">// &nb=
sp; L type L c=
ounter value type</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br><br></span><span style=3D"color: #800;" class=3D"styled-by-prett=
ify">// Everything is done at compile-time. Constexpr should work, but also=
using</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
</span><span style=3D"color: #800;" class=3D"styled-by-prettify">// `std::i=
ntegral_constant` with a template struct.</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">template</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><</span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">typename</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> T</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">></span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">constex=
pr</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">int</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> getIdForType</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">()</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> noexcept<br></span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br> </span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">constexpr</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> id</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">lastIdCounter</span><font color=3D"#666600"><span style=3D"=
color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">value</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"style=
d-by-prettify">// Get the compile-time counter's current value</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></font><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> lastIdCo=
unter</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">increment</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">();</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #800;" class=3D"styled-by-prettify">// Increment the compile-tim=
e counter - it will remember its last value</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br> </span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">return</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> id</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><font color=3D"#666600" style=3D"font-size: small;"><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></font><font=
color=3D"#666600" style=3D"font-size: small;"><span style=3D"color: #008;"=
class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> main</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
</span><span style=3D"color: #800;" class=3D"styled-by-pretti=
fy">// Whenever `getIdForType<T>()` gets called with a certain `T`, a=
value from</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br> </span><span style=3D"color: #800;" class=3D"styled-by-p=
rettify">// the compile-time counter is "bound" to that `T` for the rest of=
the program.</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br> </span><span style=3D"color: #800;" class=3D"styled-by=
-prettify">// Since it is `constexpr` and evaluated at compile-time, there'=
s no need to store</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br> </span><span style=3D"color: #800;" class=3D"styl=
ed-by-prettify">// the id anywhere in memory - calling the `getIdForType<=
;T>()` function can </span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"><br> </span><span style=3D"color: #800;" class=3D=
"styled-by-prettify">// basically be thought of as replacing </span></font>=
<span style=3D"color: #800;" class=3D"styled-by-prettify">`getIdForType<=
T>()` with a constant</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"><br></span><font color=3D"#666600" style=3D"font-size: small;=
"><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #800;" class=3D"styled-by-prettify">// at compi=
le time.</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r><br> std</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">cout </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
<<</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> g=
etIdForType</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
><</span><span style=3D"color: #606;" class=3D"styled-by-prettify">TypeA=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">>()</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify"><<</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">endl</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-b=
y-prettify">// Prints '0'</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span></font><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> std</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">cout </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify"><<</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> getIdForType</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify"><</span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">TypeB</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>>()</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify"><<</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">endl</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #800;" class=
=3D"styled-by-prettify">// Prints '1'</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br> std</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify"><<</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> getIdForType</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify"><</span><span style=3D"color: #606;" class=3D=
"styled-by-prettify">TypeC</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">>()</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y"><<</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">endl</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #800;" class=3D"styled-by-prettify">// Prints '2'</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br></span><font color=3D"#666600"=
style=3D"font-size: small;"><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-pre=
ttify">0</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">}</span></font><f=
ont color=3D"#666600" style=3D"font-size: small;"><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br></span></font></div></code></div><br><=
/div><div><br></div><div><br></div><div>Thoughts? </div><div>What moti=
vated me to write this post is the fact that we have all necessary informat=
ion at compile-time to assign an unique constant id to a specific type - bu=
t there (apparently) is no way of doing so. <br></div><div><br></div><=
/div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_117_608700932.1411134167813--
.
Author: TONGARI J <tongari95@gmail.com>
Date: Fri, 19 Sep 2014 22:07:16 +0800
Raw View
--20cf30223fc75cb59f05036b9e02
Content-Type: text/plain; charset=UTF-8
2014-09-19 21:42 GMT+08:00 Vittorio Romeo <vittorio.romeo.vee@gmail.com>:
> *Introduction*
> There have been many occasions in my games/libraries where I need to
> assign a *`0..n` *compile-time integer to a series of types. Here are two
> examples:
>
> - In one of my component-based entity management systems, I assign an
> *`std::bitset`* to every entity, where every bit corresponds to a
> specific component type.
> - One of my polymorphic memory management/recycling classes stores
> several "chunks" of allocated memory for specific types that are part of
> the same hierarchy.
> To quickly retrieve the chunks, they're stored in a contiguous array.
> Every index of the array corresponds to a chunk of a specific type.
>
> The problem here is assigning an unique integer, starting from zero, to a
> specific type, and maintaining it throughout the whole program. This
> requires keeping track of the last used integer.
>
>
>
> *Current solution/workaround*
> Sacrificing performance, it is currently possible to achieve what I
> described above. Not at compile-time, but at run-time instead.
>
> // Statically store the last used global unique id.
> inline int getLastId() noexcept
> {
> static int lastId{0};
> return lastId++;
> }
>
> // Statically store an unique id for a specific type `T`.
> template<typename T> int getIdForType() noexcept
> {
> static int id{getLastId()};
> return id;
> }
>
> struct TypeA { };
> struct TypeB { };
> struct TypeC { };
>
> int main()
> {
> // Whenever `getIdForType<T>()` gets instantiated,
> // an unique id is "bound" to `T` for the rest of the program.
>
> std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
> std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
>
> return 0;
> }
>
> The workaround allows me to implement the aforementioned examples.
> However, it requires *`static`* storage for something that could be
> determined at compile-time.
> It also requires overhead to access this storage every time we need to get
> a type's unique id.
>
>
>
> *Possible proposal*
> Implement a stateful compile-time *`std::integral_counter` *class that
> somehow *(compiler magic?) *remembers its value.
>
> // We need to "store" a counter during compile-time.
> // A new keyword/contextual keyword combination is probably required for
> this.
>
> // We basically "allocate" memory for this counter during compilation and
> // use the counter during compilation. The counter should not exist anymore
> // at run-time (or be accessible at run-time).
>
> static template std::integral_counter<int, 0> lastIdCounter;
> // ____________ _____________________ ___ _ _____________
> // | | | | L name
> // L contextual keyword combination | |
> // | | L counter starting value
> // L type L counter value type
>
> // Everything is done at compile-time. Constexpr should work, but also
> using
> // `std::integral_constant` with a template struct.
> template<typename T> constexpr int getIdForType() noexcept
> {
> constexpr int id{lastIdCounter::value}; // Get the compile-time
> counter's current value
> lastIdCounter::increment(); // Increment the compile-time counter -
> it will remember its last value
> return id;
> }
>
> int main()
> {
> // Whenever `getIdForType<T>()` gets called with a certain `T`, a
> value from
> // the compile-time counter is "bound" to that `T` for the rest of
> the program.
> // Since it is `constexpr` and evaluated at compile-time, there's no
> need to store
> // the id anywhere in memory - calling the `getIdForType<T>()`
> function can
> // basically be thought of as replacing `getIdForType<T>()` with a
> constant
> // at compile time.
>
> std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
> std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
>
> return 0;
> }
>
>
>
> Thoughts?
> What motivated me to write this post is the fact that we have all
> necessary information at compile-time to assign an unique constant id to a
> specific type - but there (apparently) is no way of doing so.
>
Q: What happens if used in different TUs?
// in file a.cpp
std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
// in file b.cpp
std::cout << getIdForType<TypeC>() << std::endl; // Prints '0'
std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
std::cout << getIdForType<TypeA>() << std::endl; // Prints '2'
Is that the expected result?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--20cf30223fc75cb59f05036b9e02
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">2014=
-09-19 21:42 GMT+08:00 Vittorio Romeo <span dir=3D"ltr"><<a href=3D"mail=
to:vittorio.romeo.vee@gmail.com" target=3D"_blank">vittorio.romeo.vee@gmail=
..com</a>></span>:<br><blockquote class=3D"gmail_quote" style=3D"margin:0=
px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);b=
order-left-style:solid;padding-left:1ex"><div dir=3D"ltr"><font size=3D"4">=
<b>Introduction</b></font><div><font>There have been many occasions in my g=
ames/libraries where I need to assign a </font><b style=3D"font-size:small"=
>`0..n` </b><font>compile-time integer to a series of types. Here are two e=
xamples:</font><br><ul style=3D"font-size:small"><li><span style=3D"line-he=
ight:normal">In one of my component-based entity management systems, I assi=
gn an <b>`std::bitset`</b>=C2=A0to every entity, where every bit correspond=
s to a specific component type.</span></li><li><span style=3D"line-height:n=
ormal">One of my polymorphic memory management/recycling classes stores sev=
eral "chunks" of allocated memory for specific types that are par=
t of the same hierarchy. <br>To quickly retrieve the chunks, they're st=
ored in a contiguous array. Every index of the array corresponds to a chunk=
of a specific type.</span></li></ul><div style=3D"font-size:small">The pro=
blem here is assigning an unique integer, starting from zero, to a specific=
type, and maintaining it throughout the whole program. This requires keepi=
ng track of the last used integer.</div><div style=3D"font-size:small"><br>=
</div><div style=3D"font-size:small"><br></div><div style=3D"font-size:smal=
l"><br></div><div style=3D"font-size:small"><b style=3D"font-size:large">Cu=
rrent solution/workaround</b><br></div><div style=3D"font-size:small">Sacri=
ficing performance, it is currently possible to achieve what I described ab=
ove. Not at compile-time, but at run-time instead.</div><div style=3D"font-=
size:small"><br></div><div style=3D"font-size:small"><div style=3D"border:1=
px solid rgb(187,187,187);word-wrap:break-word;background-color:rgb(250,250=
,250)"><code><div><span style=3D"color:rgb(136,0,0)">// Statically store th=
e last used global unique id.</span><span style=3D"color:rgb(0,0,0)"><br></=
span><span style=3D"color:rgb(0,0,136)">inline</span><span style=3D"color:r=
gb(0,0,0)"> </span><span style=3D"color:rgb(0,0,136)">int</span><span style=
=3D"color:rgb(0,0,0)"> getLastId</span><span style=3D"color:rgb(102,102,0)"=
>()</span><span style=3D"color:rgb(0,0,0)"> noexcept<br></span><span style=
=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=
=A0 =C2=A0</span><span style=3D"color:rgb(0,0,136)">static</span><span styl=
e=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,0,136)">int</span=
><span style=3D"color:rgb(0,0,0)"> lastId</span><span style=3D"color:rgb(10=
2,102,0)">{</span><span style=3D"color:rgb(0,102,102)">0</span><span style=
=3D"color:rgb(102,102,0)">};</span><span style=3D"color:rgb(0,0,0)"><br>=C2=
=A0 =C2=A0</span><span style=3D"color:rgb(0,0,136)">return</span><span styl=
e=3D"color:rgb(0,0,0)"> lastId</span><span style=3D"color:rgb(102,102,0)">+=
+;</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:r=
gb(102,102,0)">}</span><span style=3D"color:rgb(0,0,0)"><br><br></span><spa=
n style=3D"color:rgb(136,0,0)">// Statically store an unique id for a speci=
fic type `T`.</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=
=3D"color:rgb(0,0,136)">template</span><span style=3D"color:rgb(102,102,0)"=
><</span><span style=3D"color:rgb(0,0,136)">typename</span><span style=
=3D"color:rgb(0,0,0)"> T</span><span style=3D"color:rgb(102,102,0)">></s=
pan><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,0,1=
36)">int</span><span style=3D"color:rgb(0,0,0)"> getIdForType</span><span s=
tyle=3D"color:rgb(102,102,0)">()</span><span style=3D"color:rgb(0,0,0)"> no=
except<br></span><span style=3D"color:rgb(102,102,0)">{</span><span style=
=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0=
,136)">static</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D=
"color:rgb(0,0,136)">int</span><span style=3D"color:rgb(0,0,0)"> id</span><=
span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)=
">getLastId</span><span style=3D"color:rgb(102,102,0)">()};</span><span sty=
le=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0=
,0,136)">return</span><span style=3D"color:rgb(0,0,0)"> id</span><span styl=
e=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br></s=
pan><span style=3D"color:rgb(102,102,0)">}</span><span style=3D"color:rgb(0=
,0,0)"><br><br></span><span style=3D"color:rgb(0,0,136)">struct</span><span=
style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,0,102)">Ty=
peA</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb=
(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"> </span><span style=
=3D"color:rgb(102,102,0)">};</span><span style=3D"color:rgb(0,0,0)"><br></s=
pan><span style=3D"color:rgb(0,0,136)">struct</span><span style=3D"color:rg=
b(0,0,0)"> </span><span style=3D"color:rgb(102,0,102)">TypeB</span><span st=
yle=3D"color:rgb(0,0,0)"> </span><font color=3D"#666600"><span style=3D"col=
or:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"> </span><span s=
tyle=3D"color:rgb(102,102,0)">};</span><span style=3D"color:rgb(0,0,0)"><br=
></span></font><span style=3D"color:rgb(0,0,136)">struct</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,0,102)">TypeC</s=
pan><span style=3D"color:rgb(0,0,0)"> </span><font color=3D"#666600"><span =
style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"> </=
span><span style=3D"color:rgb(102,102,0)">};</span><span style=3D"color:rgb=
(0,0,0)"><br></span></font><font color=3D"#666600"><span style=3D"color:rgb=
(0,0,0)"><br></span><span style=3D"color:rgb(0,0,136)">int</span><span styl=
e=3D"color:rgb(0,0,0)"> main</span><span style=3D"color:rgb(102,102,0)">()<=
/span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(=
102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </sp=
an><span style=3D"color:rgb(136,0,0)">// Whenever `getIdForType<T>()`=
gets instantiated, </span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=
=A0 </span><span style=3D"color:rgb(136,0,0)">// an unique id is "boun=
d" to `T` for the rest of the program.</span><span style=3D"color:rgb(=
0,0,0)"><br><br>=C2=A0 =C2=A0 std</span><span style=3D"color:rgb(102,102,0)=
">::</span><span style=3D"color:rgb(0,0,0)">cout </span><span style=3D"colo=
r:rgb(102,102,0)"><<</span><span style=3D"color:rgb(0,0,0)"> getIdFor=
Type</span><span style=3D"color:rgb(102,102,0)"><</span><span style=3D"c=
olor:rgb(102,0,102)">TypeA</span><span style=3D"color:rgb(102,102,0)">>(=
)</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(1=
02,102,0)"><<</span><span style=3D"color:rgb(0,0,0)"> std</span><span=
style=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">e=
ndl</span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"color=
:rgb(0,0,0)"> </span><span style=3D"color:rgb(136,0,0)">// Prints '0=
9;</span><span style=3D"color:rgb(0,0,0)"><br></span></font><span style=3D"=
color:rgb(0,0,0)">=C2=A0 =C2=A0 std</span><span style=3D"color:rgb(102,102,=
0)">::</span><span style=3D"color:rgb(0,0,0)">cout </span><span style=3D"co=
lor:rgb(102,102,0)"><<</span><span style=3D"color:rgb(0,0,0)"> getIdF=
orType</span><span style=3D"color:rgb(102,102,0)"><</span><span style=3D=
"color:rgb(102,0,102)">TypeB</span><span style=3D"color:rgb(102,102,0)">>=
;()</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb=
(102,102,0)"><<</span><span style=3D"color:rgb(0,0,0)"> std</span><sp=
an style=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)"=
>endl</span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"col=
or:rgb(0,0,0)"> </span><span style=3D"color:rgb(136,0,0)">// Prints '1&=
#39;</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 std</span><sp=
an style=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)"=
>cout </span><span style=3D"color:rgb(102,102,0)"><<</span><span styl=
e=3D"color:rgb(0,0,0)"> getIdForType</span><span style=3D"color:rgb(102,102=
,0)"><</span><span style=3D"color:rgb(102,0,102)">TypeC</span><span styl=
e=3D"color:rgb(102,102,0)">>()</span><span style=3D"color:rgb(0,0,0)"> <=
/span><span style=3D"color:rgb(102,102,0)"><<</span><span style=3D"co=
lor:rgb(0,0,0)"> std</span><span style=3D"color:rgb(102,102,0)">::</span><s=
pan style=3D"color:rgb(0,0,0)">endl</span><span style=3D"color:rgb(102,102,=
0)">;</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:r=
gb(136,0,0)">// Prints '2'</span><span style=3D"color:rgb(0,0,0)"><=
br></span><font color=3D"#666600"><span style=3D"color:rgb(0,0,0)"><br>=C2=
=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">return</span><span sty=
le=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,102,102)">0</spa=
n><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0=
,0)"><br></span><span style=3D"color:rgb(102,102,0)">}</span></font></div><=
/code></div><br>The workaround allows me to implement the aforementioned ex=
amples. However, it requires <b>`static`</b> storage for something that cou=
ld be determined at compile-time.=C2=A0</div><div style=3D"font-size:small"=
>It also requires overhead to access this storage every time we need to get=
a type's unique id.</div><div style=3D"font-size:small"><br></div><div=
style=3D"font-size:small"><br></div><div style=3D"font-size:small"><br></d=
iv><div><font size=3D"4"><b>Possible proposal</b></font></div><div>Implemen=
t a stateful compile-time=C2=A0<b>`std::integral_counter` </b>class that so=
mehow <i>(compiler magic?) </i>remembers its value.</div><div><br></div><di=
v><div style=3D"border:1px solid rgb(187,187,187);word-wrap:break-word;back=
ground-color:rgb(250,250,250)"><code><div><span style=3D"color:rgb(136,0,0)=
">// We need to "store" a counter during compile-time.</span><spa=
n style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">=
// A new keyword/contextual keyword combination is probably required for th=
is.</span><span style=3D"color:rgb(0,0,0)"><br><br></span><span style=3D"co=
lor:rgb(136,0,0)">// We basically "allocate" memory for this coun=
ter during compilation and</span><span style=3D"color:rgb(0,0,0)"><br></spa=
n><span style=3D"color:rgb(136,0,0)">// use the counter during compilation.=
The counter should not exist anymore</span><span style=3D"color:rgb(0,0,0)=
"><br></span><span style=3D"color:rgb(136,0,0)">// at run-time (or be acces=
sible at run-time).</span><span style=3D"color:rgb(0,0,0)"><br><br></span><=
span style=3D"color:rgb(0,0,136)">static</span><span style=3D"color:rgb(0,0=
,0)"> </span><span style=3D"color:rgb(0,0,136)">template</span><span style=
=3D"color:rgb(0,0,0)"> std</span><span style=3D"color:rgb(102,102,0)">::</s=
pan><span style=3D"color:rgb(0,0,0)">integral_counter</span><span style=3D"=
color:rgb(102,102,0)"><</span><span style=3D"color:rgb(0,0,136)">int</sp=
an><span style=3D"color:rgb(102,102,0)">,</span><span style=3D"color:rgb(0,=
0,0)"> </span><span style=3D"color:rgb(0,102,102)">0</span><span style=3D"c=
olor:rgb(102,102,0)">></span><span style=3D"color:rgb(0,0,0)"> lastIdCou=
nter</span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"colo=
r:rgb(0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">// ____________=
_____________________ ___ =C2=A0_ =C2=A0_____________</span><span style=3D=
"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">// | =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0| =C2=A0L name</span><spa=
n style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">=
// L contextual keyword combination =C2=A0 | =C2=A0 =C2=A0|</span><span sty=
le=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">// =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0L counter star=
ting value</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D=
"color:rgb(136,0,0)">// =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0L t=
ype =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0L counter value =
type</span><span style=3D"color:rgb(0,0,0)"><br><br></span><span style=3D"c=
olor:rgb(136,0,0)">// Everything is done at compile-time. Constexpr should =
work, but also using</span><span style=3D"color:rgb(0,0,0)"><br></span><spa=
n style=3D"color:rgb(136,0,0)">// `std::integral_constant` with a template =
struct.</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"co=
lor:rgb(0,0,136)">template</span><span style=3D"color:rgb(102,102,0)"><<=
/span><span style=3D"color:rgb(0,0,136)">typename</span><span style=3D"colo=
r:rgb(0,0,0)"> T</span><span style=3D"color:rgb(102,102,0)">></span><spa=
n style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,0,136)">con=
stexpr</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:=
rgb(0,0,136)">int</span><span style=3D"color:rgb(0,0,0)"> getIdForType</spa=
n><span style=3D"color:rgb(102,102,0)">()</span><span style=3D"color:rgb(0,=
0,0)"> noexcept<br></span><span style=3D"color:rgb(102,102,0)">{</span><spa=
n style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:=
rgb(0,0,136)">constexpr</span><span style=3D"color:rgb(0,0,0)"> </span><spa=
n style=3D"color:rgb(0,0,136)">int</span><span style=3D"color:rgb(0,0,0)"> =
id</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:=
rgb(0,0,0)">lastIdCounter</span><font color=3D"#666600"><span style=3D"colo=
r:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">value</span><sp=
an style=3D"color:rgb(102,102,0)">};</span><span style=3D"color:rgb(0,0,0)"=
> </span><span style=3D"color:rgb(136,0,0)">// Get the compile-time counter=
's current value</span><span style=3D"color:rgb(0,0,0)"><br></span></fo=
nt><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 lastIdCounter</span><span=
style=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">i=
ncrement</span><span style=3D"color:rgb(102,102,0)">();</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(136,0,0)">// Increme=
nt the compile-time counter - it will remember its last value</span><span s=
tyle=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb=
(0,0,136)">return</span><span style=3D"color:rgb(0,0,0)"> id</span><span st=
yle=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br><=
/span><span style=3D"color:rgb(102,102,0)">}</span><span style=3D"color:rgb=
(0,0,0)"><br></span><font color=3D"#666600" style=3D"font-size:small"><span=
style=3D"color:rgb(0,0,0)"><br></span></font><font color=3D"#666600" style=
=3D"font-size:small"><span style=3D"color:rgb(0,0,136)">int</span><span sty=
le=3D"color:rgb(0,0,0)"> main</span><span style=3D"color:rgb(102,102,0)">()=
</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb=
(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </s=
pan><span style=3D"color:rgb(136,0,0)">// Whenever `getIdForType<T>()=
` gets called with a certain `T`, a value from</span><span style=3D"color:r=
gb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(136,0,0)">// t=
he compile-time counter is "bound" to that `T` for the rest of th=
e program.</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span>=
<span style=3D"color:rgb(136,0,0)">// Since it is `constexpr` and evaluated=
at compile-time, there's no need to store</span><span style=3D"color:r=
gb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(136,0,0)">// t=
he id anywhere in memory - calling the `getIdForType<T>()` function c=
an </span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span s=
tyle=3D"color:rgb(136,0,0)">// basically be thought of as replacing </span>=
</font><span style=3D"color:rgb(136,0,0)">`getIdForType<T>()` with a =
constant</span><span style=3D"color:rgb(0,0,0)"><br></span><font color=3D"#=
666600" style=3D"font-size:small"><span style=3D"color:rgb(0,0,0)">=C2=A0 =
=C2=A0 </span><span style=3D"color:rgb(136,0,0)">// at compile time.</span>=
<span style=3D"color:rgb(0,0,0)"><br><br>=C2=A0 =C2=A0 std</span><span styl=
e=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">cout <=
/span><span style=3D"color:rgb(102,102,0)"><<</span><span style=3D"co=
lor:rgb(0,0,0)"> getIdForType</span><span style=3D"color:rgb(102,102,0)">&l=
t;</span><span style=3D"color:rgb(102,0,102)">TypeA</span><span style=3D"co=
lor:rgb(102,102,0)">>()</span><span style=3D"color:rgb(0,0,0)"> </span><=
span style=3D"color:rgb(102,102,0)"><<</span><span style=3D"color:rgb=
(0,0,0)"> std</span><span style=3D"color:rgb(102,102,0)">::</span><span sty=
le=3D"color:rgb(0,0,0)">endl</span><span style=3D"color:rgb(102,102,0)">;</=
span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(136,=
0,0)">// Prints '0'</span><span style=3D"color:rgb(0,0,0)"><br></sp=
an></font><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 std</span><span st=
yle=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">cout=
</span><span style=3D"color:rgb(102,102,0)"><<</span><span style=3D"=
color:rgb(0,0,0)"> getIdForType</span><span style=3D"color:rgb(102,102,0)">=
<</span><span style=3D"color:rgb(102,0,102)">TypeB</span><span style=3D"=
color:rgb(102,102,0)">>()</span><span style=3D"color:rgb(0,0,0)"> </span=
><span style=3D"color:rgb(102,102,0)"><<</span><span style=3D"color:r=
gb(0,0,0)"> std</span><span style=3D"color:rgb(102,102,0)">::</span><span s=
tyle=3D"color:rgb(0,0,0)">endl</span><span style=3D"color:rgb(102,102,0)">;=
</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(13=
6,0,0)">// Prints '1'</span><span style=3D"color:rgb(0,0,0)"><br>=
=C2=A0 =C2=A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><span=
style=3D"color:rgb(0,0,0)">cout </span><span style=3D"color:rgb(102,102,0)=
"><<</span><span style=3D"color:rgb(0,0,0)"> getIdForType</span><span=
style=3D"color:rgb(102,102,0)"><</span><span style=3D"color:rgb(102,0,1=
02)">TypeC</span><span style=3D"color:rgb(102,102,0)">>()</span><span st=
yle=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)"><&=
lt;</span><span style=3D"color:rgb(0,0,0)"> std</span><span style=3D"color:=
rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">endl</span><span =
style=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"> </=
span><span style=3D"color:rgb(136,0,0)">// Prints '2'</span><span s=
tyle=3D"color:rgb(0,0,0)"><br></span><font color=3D"#666600" style=3D"font-=
size:small"><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span=
style=3D"color:rgb(0,0,136)">return</span><span style=3D"color:rgb(0,0,0)"=
> </span><span style=3D"color:rgb(0,102,102)">0</span><span style=3D"color:=
rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br></span><span s=
tyle=3D"color:rgb(102,102,0)">}</span></font><font color=3D"#666600" style=
=3D"font-size:small"><span style=3D"color:rgb(0,0,0)"><br></span></font></d=
iv></code></div><br></div><div><br></div><div><br></div><div>Thoughts?=C2=
=A0</div><div>What motivated me to write this post is the fact that we have=
all necessary information at compile-time to assign an unique constant id =
to a specific type - but there (apparently) is no way of doing so.=C2=A0</d=
iv></div></div></blockquote><div><br></div><div>Q: What happens if used in =
different TUs?</div><div><br></div><div class=3D"gmail_quote">=C2=A0 =C2=A0=
// in file a.cpp</div><div class=3D"gmail_quote">=C2=A0 =C2=A0 std::cout &=
lt;< getIdForType<TypeA>() << std::endl; // Prints '0=
9;</div><div class=3D"gmail_quote">=C2=A0 =C2=A0 std::cout << getIdFo=
rType<TypeB>() << std::endl; // Prints '1'</div><div cl=
ass=3D"gmail_quote">=C2=A0 =C2=A0 std::cout << getIdForType<TypeC&=
gt;() << std::endl; // Prints '2'</div><div class=3D"gmail_qu=
ote">=C2=A0 =C2=A0=C2=A0</div><div class=3D"gmail_quote">=C2=A0 =C2=A0 // i=
n file b.cpp</div><div class=3D"gmail_quote">=C2=A0 =C2=A0 std::cout <&l=
t; getIdForType<TypeC>() << std::endl; // Prints '0'</d=
iv><div class=3D"gmail_quote">=C2=A0 =C2=A0 std::cout << getIdForType=
<TypeB>() << std::endl; // Prints '1'</div><div class=
=3D"gmail_quote">=C2=A0 =C2=A0 std::cout << getIdForType<TypeA>=
() << std::endl; // Prints '2'</div><div class=3D"gmail_quote=
"><br></div><div class=3D"gmail_quote">Is that the expected result?</div></=
div></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--20cf30223fc75cb59f05036b9e02--
.
Author: Matheus Izvekov <mizvekov@gmail.com>
Date: Fri, 19 Sep 2014 07:12:39 -0700 (PDT)
Raw View
------=_Part_1255_1436504375.1411135959599
Content-Type: text/plain; charset=UTF-8
What about using typeid / std::typeinfo hash_code?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_1255_1436504375.1411135959599
Content-Type: text/html; charset=UTF-8
<div dir="ltr">What about using typeid / std::typeinfo hash_code?<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />
------=_Part_1255_1436504375.1411135959599--
.
Author: Vittorio Romeo <vittorio.romeo.vee@gmail.com>
Date: Fri, 19 Sep 2014 07:24:19 -0700 (PDT)
Raw View
------=_Part_49_1250758209.1411136659439
Content-Type: text/plain; charset=UTF-8
On Friday, 19 September 2014 16:07:25 UTC+2, TONGARI J wrote:
> Q: What happens if used in different TUs?
>
> // in file a.cpp
> std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
> std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
>
> // in file b.cpp
> std::cout << getIdForType<TypeC>() << std::endl; // Prints '0'
> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
> std::cout << getIdForType<TypeA>() << std::endl; // Prints '2'
>
> Is that the expected result?
>
No - the expected result is 0, 1, 2, 3, 4, 5.
An unique id has to be "bound" to a type for the rest of the program,
regardless of the TU.
-----
On Friday, 19 September 2014 16:12:39 UTC+2, Matheus Izvekov wrote:
>
> What about using typeid / std::typeinfo hash_code?
>
*`hash_code`* does not return an unique number in a specific range (cannot
be used for array indices, for example).
*`hash_code`* does not guarantee unique hashes - two types can have the
same hash.
*`hash_code`* is not constexpr or compile-time.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_49_1250758209.1411136659439
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><font size=3D"1">On Friday, 19 September 2014 16:07:2=
5 UTC+2, TONGARI J wrote:<br></font></div><blockquote class=3D"gmail_quote"=
style=3D"margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-co=
lor: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div=
dir=3D"ltr"><div class=3D"gmail_quote"><div><font size=3D"1">Q: What happe=
ns if used in different TUs?</font></div><div><font size=3D"1"><br></font><=
/div><div class=3D"gmail_quote"><font size=3D"1"> // in file a=
..cpp</font></div><div class=3D"gmail_quote"><font size=3D"1"> =
std::cout << getIdForType<TypeA>() << std::endl; // Print=
s '0'</font></div><div class=3D"gmail_quote"><font size=3D"1"> =
std::cout << getIdForType<TypeB>() << std::endl; // Prin=
ts '1'</font></div><div class=3D"gmail_quote"><font size=3D"1">  =
; std::cout << getIdForType<TypeC>() << std::endl; // Pri=
nts '2'</font></div><div class=3D"gmail_quote"><font size=3D"1"> &nbs=
p; </font></div><div class=3D"gmail_quote"><font size=3D"1"> &nb=
sp; // in file b.cpp</font></div><div class=3D"gmail_quote"><font size=3D"1=
"> std::cout << getIdForType<TypeC>() << std=
::endl; // Prints '0'</font></div><div class=3D"gmail_quote"><font size=3D"=
1"> std::cout << getIdForType<TypeB>() << st=
d::endl; // Prints '1'</font></div><div class=3D"gmail_quote"><font size=3D=
"1"> std::cout << getIdForType<TypeA>() << s=
td::endl; // Prints '2'</font></div><div class=3D"gmail_quote"><font size=
=3D"1"><br></font></div><div class=3D"gmail_quote"><font size=3D"1">Is that=
the expected result?</font></div></div></div></blockquote><div><br></div><=
div>No - the expected result is 0, 1, 2, 3, 4, 5.<br>An unique id has to be=
"bound" to a type for the rest of the program, regardless of the TU.</div>=
<div><br></div><div>-----</div><br><font size=3D"1">On Friday, 19 September=
2014 16:12:39 UTC+2, Matheus Izvekov wrote:</font><blockquote class=3D"gm=
ail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc soli=
d;padding-left: 1ex;"><div dir=3D"ltr"><font size=3D"1">What about using ty=
peid / std::typeinfo hash_code?</font></div></blockquote><div><br></div><di=
v><b>`hash_code`</b> does not return an unique number in a specific ra=
nge (cannot be used for array indices, for example).</div><div><b>`hash_cod=
e`</b> does not guarantee unique hashes - two types can have the same =
hash.<br></div><div><b>`hash_code`</b> is not constexpr or compile-tim=
e.<br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_49_1250758209.1411136659439--
.
Author: Tony V E <tvaneerd@gmail.com>
Date: Fri, 19 Sep 2014 10:45:03 -0400
Raw View
--089e013c655874ab0905036c2522
Content-Type: text/plain; charset=UTF-8
On Fri, Sep 19, 2014 at 10:24 AM, Vittorio Romeo <
vittorio.romeo.vee@gmail.com> wrote:
> On Friday, 19 September 2014 16:07:25 UTC+2, TONGARI J wrote:
>
>> Q: What happens if used in different TUs?
>>
>> // in file a.cpp
>> std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
>> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
>> std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
>>
>> // in file b.cpp
>> std::cout << getIdForType<TypeC>() << std::endl; // Prints '0'
>> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
>> std::cout << getIdForType<TypeA>() << std::endl; // Prints '2'
>>
>> Is that the expected result?
>>
>
> No - the expected result is 0, 1, 2, 3, 4, 5.
>
ODR says that the function getIdForType<TypeC>() only exists once, so it
will always return the same value.
I expect
0
1
2
2
1
0
(regardless of which cpp is called first (but assuming they are not called
concurrently (the counters would need to be atomic in a threading case))).
Tony
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--089e013c655874ab0905036c2522
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><div class=3D"gmail_quo=
te">On Fri, Sep 19, 2014 at 10:24 AM, Vittorio Romeo <span dir=3D"ltr"><=
<a href=3D"mailto:vittorio.romeo.vee@gmail.com" target=3D"_blank">vittorio.=
romeo.vee@gmail.com</a>></span> wrote:<br><blockquote class=3D"gmail_quo=
te" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"=
><div dir=3D"ltr"><span class=3D""><div><font size=3D"1">On Friday, 19 Sept=
ember 2014 16:07:25 UTC+2, TONGARI J wrote:<br></font></div></span><blockqu=
ote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-wid=
th:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-l=
eft:1ex"><div dir=3D"ltr"><div class=3D"gmail_quote"><span class=3D""><div>=
<font size=3D"1">Q: What happens if used in different TUs?</font></div><div=
><font size=3D"1"><br></font></div><div class=3D"gmail_quote"><font size=3D=
"1">=C2=A0 =C2=A0 // in file a.cpp</font></div></span><span class=3D""><div=
class=3D"gmail_quote"><font size=3D"1">=C2=A0 =C2=A0 std::cout << ge=
tIdForType<TypeA>() << std::endl; // Prints '0'</font><=
/div><div class=3D"gmail_quote"><font size=3D"1">=C2=A0 =C2=A0 std::cout &l=
t;< getIdForType<TypeB>() << std::endl; // Prints '1'=
;</font></div><div class=3D"gmail_quote"><font size=3D"1">=C2=A0 =C2=A0 std=
::cout << getIdForType<TypeC>() << std::endl; // Prints &=
#39;2'</font></div><div class=3D"gmail_quote"><font size=3D"1">=C2=A0 =
=C2=A0=C2=A0</font></div></span><div class=3D"gmail_quote"><font size=3D"1"=
>=C2=A0 =C2=A0 // in file b.cpp</font></div><div class=3D"gmail_quote"><fon=
t size=3D"1">=C2=A0 =C2=A0 std::cout << getIdForType<TypeC>() &=
lt;< std::endl; // Prints '0'</font></div><span class=3D""><div =
class=3D"gmail_quote"><font size=3D"1">=C2=A0 =C2=A0 std::cout << get=
IdForType<TypeB>() << std::endl; // Prints '1'</font></=
div></span><span class=3D""><div class=3D"gmail_quote"><font size=3D"1">=C2=
=A0 =C2=A0 std::cout << getIdForType<TypeA>() << std::end=
l; // Prints '2'</font></div><div class=3D"gmail_quote"><font size=
=3D"1"><br></font></div><div class=3D"gmail_quote"><font size=3D"1">Is that=
the expected result?</font></div></span></div></div></blockquote><div><br>=
</div><div>No - the expected result is 0, 1, 2, 3, 4, 5.<br></div></div></b=
lockquote><div><br></div><div>ODR says that the function getIdForType<Ty=
peC>() only exists once, so it will always return the same value.<br><br=
></div><div>I expect<br><br>0<br>1<br>2<br>2<br>1<br>0<br><br></div><div>(r=
egardless of which cpp is called first (but assuming they are not called co=
ncurrently (the counters would need to be atomic in a threading case))).<br=
><br></div><div>Tony<br></div></div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--089e013c655874ab0905036c2522--
.
Author: Vittorio Romeo <vittorio.romeo@outlook.com>
Date: Fri, 19 Sep 2014 17:02:15 +0200
Raw View
--_b0cc5975-4bab-4988-8a4f-3bb37827a16a_
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
`getIdForType<T>` should be marked as `inline` then. I forgot about it - I =
actually do it in my code (using the `static` storage method for counting i=
ds).
Date: Fri, 19 Sep 2014 10:45:03 -0400
Subject: Re: [std-proposals] Re: Compile-time static `std::integral_counter=
`
From: tvaneerd@gmail.com
To: std-proposals@isocpp.org
On Fri, Sep 19, 2014 at 10:24 AM, Vittorio Romeo <vittorio.romeo.vee@gmail.=
com> wrote:
On Friday, 19 September 2014 16:07:25 UTC+2, TONGARI J wrote:
Q: What happens if used in different TUs?
// in file a.cpp std::cout << getIdForType<TypeA>() << std::endl; //=
Prints '0' std::cout << getIdForType<TypeB>() << std::endl; // Prints '=
1' std::cout << getIdForType<TypeC>() << std::endl; // Prints '2' =
// in file b.cpp std::cout << getIdForType<TypeC>() << std::endl; // Pr=
ints '0' std::cout << getIdForType<TypeB>() << std::endl; // Prints '1' =
std::cout << getIdForType<TypeA>() << std::endl; // Prints '2'
Is that the expected result?
No - the expected result is 0, 1, 2, 3, 4, 5.
ODR says that the function getIdForType<TypeC>() only exists once, so it wi=
ll always return the same value.
I expect
0
1
2
2
1
0
(regardless of which cpp is called first (but assuming they are not called =
concurrently (the counters would need to be atomic in a threading case))).
Tony
--=20
---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
=20
--=20
---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
--_b0cc5975-4bab-4988-8a4f-3bb37827a16a_
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<style><!--
..hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class=3D'hmmessage'><div dir=3D'ltr'>`getIdForType<T>` should b=
e marked as <b>`inline`</b> then. I forgot about it - I actually do it in m=
y code (using the `static` storage method for counting ids).<br><br><div><h=
r id=3D"stopSpelling">Date: Fri, 19 Sep 2014 10:45:03 -0400<br>Subject: Re:=
[std-proposals] Re: Compile-time static `std::integral_counter`<br>From: t=
vaneerd@gmail.com<br>To: std-proposals@isocpp.org<br><br><div dir=3D"ltr"><=
br><div class=3D"ecxgmail_extra"><br><div class=3D"ecxgmail_quote">On Fri, =
Sep 19, 2014 at 10:24 AM, Vittorio Romeo <span dir=3D"ltr"><<a href=3D"m=
ailto:vittorio.romeo.vee@gmail.com" target=3D"_blank">vittorio.romeo.vee@gm=
ail.com</a>></span> wrote:<br><blockquote class=3D"ecxgmail_quote" style=
=3D"border-left:1px #ccc solid;padding-left:1ex;"><div dir=3D"ltr"><span><d=
iv><font size=3D"1">On Friday, 19 September 2014 16:07:25 UTC+2, TONGARI J =
wrote:<br></font></div></span><blockquote class=3D"ecxgmail_quote" style=3D=
"border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style=
:solid;padding-left:1ex;"><div dir=3D"ltr"><div class=3D"ecxgmail_quote"><s=
pan><div><font size=3D"1">Q: What happens if used in different TUs?</font><=
/div><div><font size=3D"1"><br></font></div><div class=3D"ecxgmail_quote"><=
font size=3D"1"> // in file a.cpp</font></div></span><span><di=
v class=3D"ecxgmail_quote"><font size=3D"1"> std::cout <<=
; getIdForType<TypeA>() << std::endl; // Prints '0'</font></div=
><div class=3D"ecxgmail_quote"><font size=3D"1"> std::cout <=
;< getIdForType<TypeB>() << std::endl; // Prints '1'</font><=
/div><div class=3D"ecxgmail_quote"><font size=3D"1"> std::cout=
<< getIdForType<TypeC>() << std::endl; // Prints '2'</fo=
nt></div><div class=3D"ecxgmail_quote"><font size=3D"1"> =
</font></div></span><div class=3D"ecxgmail_quote"><font size=3D"1"> &=
nbsp; // in file b.cpp</font></div><div class=3D"ecxgmail_quote"><font size=
=3D"1"> std::cout << getIdForType<TypeC>() <<=
; std::endl; // Prints '0'</font></div><span><div class=3D"ecxgmail_quote">=
<font size=3D"1"> std::cout << getIdForType<TypeB>=
() << std::endl; // Prints '1'</font></div></span><span><div class=3D=
"ecxgmail_quote"><font size=3D"1"> std::cout << getIdFor=
Type<TypeA>() << std::endl; // Prints '2'</font></div><div clas=
s=3D"ecxgmail_quote"><font size=3D"1"><br></font></div><div class=3D"ecxgma=
il_quote"><font size=3D"1">Is that the expected result?</font></div></span>=
</div></div></blockquote><div><br></div><div>No - the expected result is 0,=
1, 2, 3, 4, 5.<br></div></div></blockquote><div><br></div><div>ODR says th=
at the function getIdForType<TypeC>() only exists once, so it will al=
ways return the same value.<br><br></div><div>I expect<br><br>0<br>1<br>2<b=
r>2<br>1<br>0<br><br></div><div>(regardless of which cpp is called first (b=
ut assuming they are not called concurrently (the counters would need to be=
atomic in a threading case))).<br><br></div><div>Tony<br></div></div><br><=
/div></div>
<BR>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br></div> </div></body>
</html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--_b0cc5975-4bab-4988-8a4f-3bb37827a16a_--
.
Author: Thiago Macieira <thiago@macieira.org>
Date: Fri, 19 Sep 2014 08:04:25 -0700
Raw View
On Friday 19 September 2014 10:45:03 Tony V E wrote:
> ODR says that the function getIdForType<TypeC>() only exists once, so it
> will always return the same value.
>
> I expect
>
> 0
> 1
> 2
> 2
> 1
> 0
ODR is not enough. This could print:
0
1
0
0
1
0
if somehow the build selected getIdForType from different TUs for different
types.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Vittorio Romeo <vittorio.romeo.vee@gmail.com>
Date: Fri, 19 Sep 2014 08:35:46 -0700 (PDT)
Raw View
------=_Part_2489_1744765034.1411140946214
Content-Type: text/plain; charset=UTF-8
True. Inlining both functions should prevent that from happening (it
already does work as intended in my implementation that unfortunately uses
run-time static storage).
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_2489_1744765034.1411140946214
Content-Type: text/html; charset=UTF-8
<div dir="ltr">True. Inlining both functions should prevent that from happening (it already does work as intended in my implementation that unfortunately uses run-time static storage).</div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />
------=_Part_2489_1744765034.1411140946214--
.
Author: Thiago Macieira <thiago@macieira.org>
Date: Fri, 19 Sep 2014 09:56:35 -0700
Raw View
On Friday 19 September 2014 08:35:46 Vittorio Romeo wrote:
> True. Inlining both functions should prevent that from happening (it
> already does work as intended in my implementation that unfortunately uses
> run-time static storage).
No, it doesn't, since inline non-static means they are merged at link- or run-
time so that ODR is followed. Which one is selected is entirely up to the
linkers, which rightly assumes that any copy is fine.
Usually, they select all from the same TU, but they don't have to. Just try
with three TUs now, with the third one using only getIdForType<TypeC>().
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: inkwizytoryankes@gmail.com
Date: Fri, 19 Sep 2014 10:13:48 -0700 (PDT)
Raw View
------=_Part_549_128501884.1411146828715
Content-Type: text/plain; charset=UTF-8
Why don't move this types to one typedef?
typedef any_variadic_template<A, B, C, D> typeColection;
template<typename T>
constexpr int f() { return type_pos_in_param<T, typeColection>::value; }
You can query this type anywhere you want.
On Friday, September 19, 2014 3:42:48 PM UTC+2, Vittorio Romeo wrote:
>
>
> *Possible proposal*
> Implement a stateful compile-time *`std::integral_counter` *class that
> somehow *(compiler magic?) *remembers its value.
>
> // We need to "store" a counter during compile-time.
> // A new keyword/contextual keyword combination is probably required for
> this.
>
> // We basically "allocate" memory for this counter during compilation and
> // use the counter during compilation. The counter should not exist anymore
> // at run-time (or be accessible at run-time).
>
> static template std::integral_counter<int, 0> lastIdCounter;
> // ____________ _____________________ ___ _ _____________
> // | | | | L name
> // L contextual keyword combination | |
> // | | L counter starting value
> // L type L counter value type
>
> // Everything is done at compile-time. Constexpr should work, but also
> using
> // `std::integral_constant` with a template struct.
> template<typename T> constexpr int getIdForType() noexcept
> {
> constexpr int id{lastIdCounter::value}; // Get the compile-time
> counter's current value
> lastIdCounter::increment(); // Increment the compile-time counter -
> it will remember its last value
> return id;
> }
>
> int main()
> {
> // Whenever `getIdForType<T>()` gets called with a certain `T`, a
> value from
> // the compile-time counter is "bound" to that `T` for the rest of
> the program.
> // Since it is `constexpr` and evaluated at compile-time, there's no
> need to store
> // the id anywhere in memory - calling the `getIdForType<T>()`
> function can
> // basically be thought of as replacing `getIdForType<T>()` with a
> constant
> // at compile time.
>
> std::cout << getIdForType<TypeA>() << std::endl; // Prints '0'
> std::cout << getIdForType<TypeB>() << std::endl; // Prints '1'
> std::cout << getIdForType<TypeC>() << std::endl; // Prints '2'
>
> return 0;
> }
>
>
>
> Thoughts?
> What motivated me to write this post is the fact that we have all
> necessary information at compile-time to assign an unique constant id to a
> specific type - but there (apparently) is no way of doing so.
>
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_549_128501884.1411146828715
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Why don't move this types to one typedef?<br><div class=3D=
"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-color: =
rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: brea=
k-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">typedef</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> any_variadic_template</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify">A</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> B</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> C</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> D</span><span style=3D"color: #660;" class=3D"styled-by-prettify">></s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> typeColectio=
n</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">template</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">typename</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">></span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">constexpr</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> f</span><span style=3D"color: #660;" class=3D"styled-by-prettify">()</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">return</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> type_pos_in_param</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify"><code class=3D"prettyprint"><span style=
=3D"color: #000;" class=3D"styled-by-prettify">T, typeColection</span><span=
style=3D"color: #660;" class=3D"styled-by-prettify"></span></code>></sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">::value; </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div></code>=
</div><br>You can query this type anywhere you want.<br><br>On Friday, Sept=
ember 19, 2014 3:42:48 PM UTC+2, Vittorio Romeo wrote:<blockquote class=3D"=
gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc so=
lid;padding-left: 1ex;"><div dir=3D"ltr"><div><div style=3D"font-size:small=
"><br></div><div><font size=3D"4"><b>Possible proposal</b></font></div><div=
>Implement a stateful compile-time <b>`std::integral_<wbr>counter` </b=
>class that somehow <i>(compiler magic?) </i>remembers its value.</div><div=
><br></div><div><div style=3D"border:1px solid rgb(187,187,187);word-wrap:b=
reak-word;background-color:rgb(250,250,250)"><code><div><span style=3D"colo=
r:#800">// We need to "store" a counter during compile-time.</span><span st=
yle=3D"color:#000"><br></span><span style=3D"color:#800">// A new keyword/c=
ontextual keyword combination is probably required for this.</span><span st=
yle=3D"color:#000"><br><br></span><span style=3D"color:#800">// We basicall=
y "allocate" memory for this counter during compilation and</span><span sty=
le=3D"color:#000"><br></span><span style=3D"color:#800">// use the counter =
during compilation. The counter should not exist anymore</span><span style=
=3D"color:#000"><br></span><span style=3D"color:#800">// at run-time (or be=
accessible at run-time).</span><span style=3D"color:#000"><br><br></span><=
span style=3D"color:#008">static</span><span style=3D"color:#000"> </span><=
span style=3D"color:#008">template</span><span style=3D"color:#000"> std</s=
pan><span style=3D"color:#660">::</span><span style=3D"color:#000">integral=
_counter</span><span style=3D"color:#660"><</span><span style=3D"color:#=
008">int</span><span style=3D"color:#660">,</span><span style=3D"color:#000=
"> </span><span style=3D"color:#066">0</span><span style=3D"color:#660">>=
;</span><span style=3D"color:#000"> lastIdCounter</span><span style=3D"colo=
r:#660">;</span><span style=3D"color:#000"><br></span><span style=3D"color:=
#800">// ____________ _____________________ ___ _ _____________=
</span><span style=3D"color:#000"><br></span><span style=3D"color:#800">// =
| | &n=
bsp; | | L name</span=
><span style=3D"color:#000"><br></span><span style=3D"color:#800">// L cont=
extual keyword combination | |</span><span style=3D"col=
or:#000"><br></span><span style=3D"color:#800">// &nbs=
p; | &=
nbsp; | L counter starting value</span><span sty=
le=3D"color:#000"><br></span><span style=3D"color:#800">// &n=
bsp; L type &=
nbsp; L counter value type</span><span style=3D"color:#000"><b=
r><br></span><span style=3D"color:#800">// Everything is done at compile-ti=
me. Constexpr should work, but also using</span><span style=3D"color:#000">=
<br></span><span style=3D"color:#800">// `std::integral_constant` with a te=
mplate struct.</span><span style=3D"color:#000"><br></span><span style=3D"c=
olor:#008">template</span><span style=3D"color:#660"><</span><span style=
=3D"color:#008">typename</span><span style=3D"color:#000"> T</span><span st=
yle=3D"color:#660">></span><span style=3D"color:#000"> </span><span styl=
e=3D"color:#008">constexpr</span><span style=3D"color:#000"> </span><span s=
tyle=3D"color:#008">int</span><span style=3D"color:#000"> getIdForType</spa=
n><span style=3D"color:#660">()</span><span style=3D"color:#000"> noexcept<=
br></span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br=
> </span><span style=3D"color:#008">constexpr</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#008">int</span><span style=
=3D"color:#000"> id</span><span style=3D"color:#660">{</span><span style=3D=
"color:#000">lastIdCounter</span><font color=3D"#666600"><span style=3D"col=
or:#660">::</span><span style=3D"color:#000">value</span><span style=3D"col=
or:#660">};</span><span style=3D"color:#000"> </span><span style=3D"color:#=
800">// Get the compile-time counter's current value</span><span style=3D"c=
olor:#000"><br></span></font><span style=3D"color:#000"> lastI=
dCounter</span><span style=3D"color:#660">::</span><span style=3D"color:#00=
0">increment</span><span style=3D"color:#660">();</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#800">// Increment the compile-time co=
unter - it will remember its last value</span><span style=3D"color:#000"><b=
r> </span><span style=3D"color:#008">return</span><span style=
=3D"color:#000"> id</span><span style=3D"color:#660">;</span><span style=3D=
"color:#000"><br></span><span style=3D"color:#660">}</span><span style=3D"c=
olor:#000"><br></span><font style=3D"font-size:small" color=3D"#666600"><sp=
an style=3D"color:#000"><br></span></font><font style=3D"font-size:small" c=
olor=3D"#666600"><span style=3D"color:#008">int</span><span style=3D"color:=
#000"> main</span><span style=3D"color:#660">()</span><span style=3D"color:=
#000"><br></span><span style=3D"color:#660">{</span><span style=3D"color:#0=
00"><br> </span><span style=3D"color:#800">// Whenever `getIdF=
orType<T>()` gets called with a certain `T`, a value from</span><span=
style=3D"color:#000"><br> </span><span style=3D"color:#800">/=
/ the compile-time counter is "bound" to that `T` for the rest of the progr=
am.</span><span style=3D"color:#000"><br> </span><span style=
=3D"color:#800">// Since it is `constexpr` and evaluated at compile-time, t=
here's no need to store</span><span style=3D"color:#000"><br> =
</span><span style=3D"color:#800">// the id anywhere in memory - calling th=
e `getIdForType<T>()` function can </span><span style=3D"color:#000">=
<br> </span><span style=3D"color:#800">// basically be thought=
of as replacing </span></font><span style=3D"color:#800">`getIdForType<=
T>()` with a constant</span><span style=3D"color:#000"><br></span><font =
style=3D"font-size:small" color=3D"#666600"><span style=3D"color:#000">&nbs=
p; </span><span style=3D"color:#800">// at compile time.</span><span=
style=3D"color:#000"><br><br> std</span><span style=3D"color:=
#660">::</span><span style=3D"color:#000">cout </span><span style=3D"color:=
#660"><<</span><span style=3D"color:#000"> getIdForType</span><span s=
tyle=3D"color:#660"><</span><span style=3D"color:#606">TypeA</span><span=
style=3D"color:#660">>()</span><span style=3D"color:#000"> </span><span=
style=3D"color:#660"><<</span><span style=3D"color:#000"> std</span>=
<span style=3D"color:#660">::</span><span style=3D"color:#000">endl</span><=
span style=3D"color:#660">;</span><span style=3D"color:#000"> </span><span =
style=3D"color:#800">// Prints '0'</span><span style=3D"color:#000"><br></s=
pan></font><span style=3D"color:#000"> std</span><span style=
=3D"color:#660">::</span><span style=3D"color:#000">cout </span><span style=
=3D"color:#660"><<</span><span style=3D"color:#000"> getIdForType</sp=
an><span style=3D"color:#660"><</span><span style=3D"color:#606">TypeB</=
span><span style=3D"color:#660">>()</span><span style=3D"color:#000"> </=
span><span style=3D"color:#660"><<</span><span style=3D"color:#000"> =
std</span><span style=3D"color:#660">::</span><span style=3D"color:#000">en=
dl</span><span style=3D"color:#660">;</span><span style=3D"color:#000"> </s=
pan><span style=3D"color:#800">// Prints '1'</span><span style=3D"color:#00=
0"><br> std</span><span style=3D"color:#660">::</span><span st=
yle=3D"color:#000">cout </span><span style=3D"color:#660"><<</span><s=
pan style=3D"color:#000"> getIdForType</span><span style=3D"color:#660"><=
;</span><span style=3D"color:#606">TypeC</span><span style=3D"color:#660">&=
gt;()</span><span style=3D"color:#000"> </span><span style=3D"color:#660">&=
lt;<</span><span style=3D"color:#000"> std</span><span style=3D"color:#6=
60">::</span><span style=3D"color:#000">endl</span><span style=3D"color:#66=
0">;</span><span style=3D"color:#000"> </span><span style=3D"color:#800">//=
Prints '2'</span><span style=3D"color:#000"><br></span><font style=3D"font=
-size:small" color=3D"#666600"><span style=3D"color:#000"><br> =
</span><span style=3D"color:#008">return</span><span style=3D"color:#000">=
</span><span style=3D"color:#066">0</span><span style=3D"color:#660">;</sp=
an><span style=3D"color:#000"><br></span><span style=3D"color:#660">}</span=
></font><font style=3D"font-size:small" color=3D"#666600"><span style=3D"co=
lor:#000"><br></span></font></div></code></div><br></div><div><br></div><di=
v><br></div><div>Thoughts? </div><div>What motivated me to write this =
post is the fact that we have all necessary information at compile-time to =
assign an unique constant id to a specific type - but there (apparently) is=
no way of doing so. <br></div><div><br></div></div></div></blockquote=
></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_549_128501884.1411146828715--
.
Author: David Krauss <potswa@gmail.com>
Date: Sat, 20 Sep 2014 01:23:06 +0800
Raw View
--Apple-Mail=_13A3C924-4E7B-4E2C-868C-8C08F4F55975
Content-Type: text/plain; charset=ISO-8859-1
On 2014-09-19, at 9:42 PM, Vittorio Romeo <vittorio.romeo.vee@gmail.com> wrote:
> Introduction
> There have been many occasions in my games/libraries where I need to assign a `0..n` compile-time integer to a series of types. Here are two examples:
Please see my StackOverflow Q&A, "Does C++ support compile-time counters?" (Scroll to the C++11 section.)
I see some discussion of the ODR in this thread. Any counter usage with linkage does need to be placed in the header file and repeated exactly the same in each TU. Avoid sharing a counter between headers unless those headers are all-or-nothing.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--Apple-Mail=_13A3C924-4E7B-4E2C-868C-8C08F4F55975
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;09–19, at 9:42 PM, Vittorio Romeo <<a href=3D"mailto:vittori=
o.romeo.vee@gmail.com">vittorio.romeo.vee@gmail.com</a>> wrote:</div><br=
class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div dir=3D"=
ltr"><font size=3D"4"><b>Introduction</b></font><div><font size=3D"2">There=
have been many occasions in my games/libraries where I need to assign a </=
font><b style=3D"font-size: small;">`0..n` </b><font size=3D"2">compile-tim=
e integer to a series of types. Here are two examples:</font><br></div></di=
v></blockquote><div><br></div><div><br></div><div>Please see my StackOverfl=
ow Q&A, "<a href=3D"http://stackoverflow.com/a/6174263/153285">Does C++=
support compile-time counters?</a>” (Scroll to the C++11 section.)</=
div><div><br></div><div>I see some discussion of the ODR in this thread. An=
y counter usage with linkage does need to be placed in the header file and =
repeated exactly the same in each TU. Avoid sharing a counter between heade=
rs unless those headers are all-or-nothing.</div></div><br></body></html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--Apple-Mail=_13A3C924-4E7B-4E2C-868C-8C08F4F55975--
.
Author: Vittorio Romeo <vittorio.romeo.vee@gmail.com>
Date: Fri, 19 Sep 2014 10:40:55 -0700 (PDT)
Raw View
------=_Part_2799_368633182.1411148455858
Content-Type: text/plain; charset=UTF-8
On Friday, 19 September 2014 19:13:48 UTC+2, inkwizyt...@gmail.com wrote:
>
> Why don't move this types to one typedef?
> typedef any_variadic_template<A, B, C, D> typeColection;
> template<typename T>
> constexpr int f() { return type_pos_in_param<T, typeColection>::value; }
>
> You can query this type anywhere you want.
>
This requires creating and maintaining a list of types... which it's
(should be) unnecessary and it's very cumbersome for the user of my
libraries.
---
On Friday, 19 September 2014 19:23:22 UTC+2, David Krauss wrote:
>
> I see some discussion of the ODR in this thread. Any counter usage with
> linkage does need to be placed in the header file and repeated exactly the
> same in each TU. Avoid sharing a counter between headers unless those
> headers are all-or-nothing.
>
Can you elaborate more on this? What I am currently doing is putting the
workaround code in an header file (header-only library) and using inline in
every function. Is that not guaranteed to return expected values in
different TUs? (values that never repeat, I mean)
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_2799_368633182.1411148455858
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><font size=3D"1">On Friday, 19 September 2014 19:13:48 UTC=
+2, inkwizyt...@gmail.com wrote:</font><blockquote class=3D"gmail_quote" st=
yle=3D"margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color=
: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div di=
r=3D"ltr"><font size=3D"1">Why don't move this types to one typedef?<br></f=
ont><div style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-wo=
rd; background-color: rgb(250, 250, 250);"><code><font size=3D"1"><span sty=
le=3D"color: rgb(0, 0, 136);">typedef</span><span style=3D"color: rgb(0, 0,=
0);"> any_variadic_template</span><span style=3D"color: rgb(102, 102,=
0);"><</span><span style=3D"color: rgb(0, 0, 0);">A</span><span style=
=3D"color: rgb(102, 102, 0);">,</span><span style=3D"color: rgb(0, 0, 0);">=
B</span><span style=3D"color: rgb(102, 102, 0);">,</span><span style=
=3D"color: rgb(0, 0, 0);"> C</span><span style=3D"color: rgb(102, 102,=
0);">,</span><span style=3D"color: rgb(0, 0, 0);"> D</span><span styl=
e=3D"color: rgb(102, 102, 0);">></span><span style=3D"color: rgb(0, 0, 0=
);"> typeColection</span><span style=3D"color: rgb(102, 102, 0);">;</s=
pan><span style=3D"color: rgb(0, 0, 0);"><br></span><span style=3D"color: r=
gb(0, 0, 136);">template</span><span style=3D"color: rgb(102, 102, 0);"><=
;</span><span style=3D"color: rgb(0, 0, 136);">typename</span><span style=
=3D"color: rgb(0, 0, 0);"> T</span><span style=3D"color: rgb(102, 102,=
0);">></span><span style=3D"color: rgb(0, 0, 0);"><br></span><span styl=
e=3D"color: rgb(0, 0, 136);">constexpr</span><span style=3D"color: rgb(0, 0=
, 0);"> </span><span style=3D"color: rgb(0, 0, 136);">int</span><span =
style=3D"color: rgb(0, 0, 0);"> f</span><span style=3D"color: rgb(102,=
102, 0);">()</span><span style=3D"color: rgb(0, 0, 0);"> </span><span=
style=3D"color: rgb(102, 102, 0);">{</span><span style=3D"color: rgb(0, 0,=
0);"> </span><span style=3D"color: rgb(0, 0, 136);">return</span><spa=
n style=3D"color: rgb(0, 0, 0);"> type_pos_in_param</span><span style=
=3D"color: rgb(102, 102, 0);"><</span><span style=3D"color: rgb(102, 102=
, 0);"><code><span style=3D"color: rgb(0, 0, 0);">T, typeColection</span></=
code>></span><span style=3D"color: rgb(0, 0, 0);">::value; </span><=
span style=3D"color: rgb(102, 102, 0);">}</span><span style=3D"color: rgb(0=
, 0, 0);"><br></span></font></code></div><font size=3D"1"><br>You can query=
this type anywhere you want.</font></div></blockquote><div><br></div><div>=
This requires creating and maintaining a list of types... which it's (shoul=
d be) unnecessary and it's very cumbersome for the user of my libraries.</d=
iv><div><br></div><div>---</div><br><font size=3D"1">On Friday, 19 Septembe=
r 2014 19:23:22 UTC+2, David Krauss wrote:</font><blockquote class=3D"gmai=
l_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;=
padding-left: 1ex;"><div style=3D"word-wrap:break-word"><div><div><font siz=
e=3D"1">I see some discussion of the ODR in this thread. Any counter usage =
with linkage does need to be placed in the header file and repeated exactly=
the same in each TU. Avoid sharing a counter between headers unless those =
headers are all-or-nothing.</font></div></div></div></blockquote><div><br><=
/div><div>Can you elaborate more on this? What I am currently doing is putt=
ing the workaround code in an header file (header-only library) and using i=
nline in every function. Is that not guaranteed to return expected values i=
n different TUs? (values that never repeat, I mean)</div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_2799_368633182.1411148455858--
.
Author: Sean Middleditch <sean.middleditch@gmail.com>
Date: Fri, 19 Sep 2014 11:49:07 -0700 (PDT)
Raw View
------=_Part_1764_1967174671.1411152547646
Content-Type: text/plain; charset=UTF-8
On Friday, September 19, 2014 7:12:39 AM UTC-7, Matheus Izvekov wrote:
>
> What about using typeid / std::typeinfo hash_code?
>
Embedded and games very frequently turn off RTTI. It might be more
palatable if the user had direct control over which types had RTTI and
which don't (we don't need every polymorphic type to have the info).
One of the main uses for the feature that Vittorio is presenting here is in
developing custom RTTI-like data structures that (a) provide explicit
control and (b) have useful information that RTTI lacks (which may be
high-level application semantic information that C++ itself won't and can't
even know about).
This seemed to be something that a lot of people got surprised by at CppCon
so perhaps it bears repeating: a very significant set of C++-using
industries usually will not (often /cannot/) use exceptions or RTTI or
virtual inheritance or a few certain other C++ language features. RTTI is
not a solution, especially in Vittorio's stated domain (games).
That said... I don't think Vittorio's feature request is possible. There's
no way to give stable compile-time IDs across translation units in the
usage pattern he wants them in, even with a new magic language construct.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_1764_1967174671.1411152547646
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Friday, September 19, 2014 7:12:39 AM UTC-7, Matheus Iz=
vekov wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-lef=
t: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">W=
hat about using typeid / std::typeinfo hash_code?<br></div></blockquote><di=
v><br></div><div>Embedded and games very frequently turn off RTTI. It might=
be more palatable if the user had direct control over which types had RTTI=
and which don't (we don't need every polymorphic type to have the info).</=
div><div><br></div><div>One of the main uses for the feature that Vittorio =
is presenting here is in developing custom RTTI-like data structures that (=
a) provide explicit control and (b) have useful information that RTTI lacks=
(which may be high-level application semantic information that C++ itself =
won't and can't even know about).</div><div><br></div><div>This seemed to b=
e something that a lot of people got surprised by at CppCon so perhaps it b=
ears repeating: a very significant set of C++-using industries usually will=
not (often /cannot/) use exceptions or RTTI or virtual inheritance or a fe=
w certain other C++ language features. RTTI is not a solution, especially i=
n Vittorio's stated domain (games).</div><div><br></div><div>That said... I=
don't think Vittorio's feature request is possible. There's no way to give=
stable compile-time IDs across translation units in the usage pattern he w=
ants them in, even with a new magic language construct.</div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_1764_1967174671.1411152547646--
.
Author: David Krauss <potswa@gmail.com>
Date: Sat, 20 Sep 2014 07:32:29 +0800
Raw View
--Apple-Mail=_48C506D6-33DF-4AC5-9FC9-9883D2C39E0D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
On 2014-09-20, at 1:40 AM, Vittorio Romeo <vittorio.romeo.vee@gmail.com> wr=
ote:
> On Friday, 19 September 2014 19:23:22 UTC+2, David Krauss wrote:
> I see some discussion of the ODR in this thread. Any counter usage with l=
inkage does need to be placed in the header file and repeated exactly the s=
ame in each TU. Avoid sharing a counter between headers unless those header=
s are all-or-nothing.
>=20
> Can you elaborate more on this? What I am currently doing is putting the =
workaround code in an header file (header-only library) and using inline in=
every function. Is that not guaranteed to return expected values in differ=
ent TUs? (values that never repeat, I mean)
It's in reference to the StackOverflow answer.
If the counter values are assigned at runtime, it's sufficient to have exte=
rnal linkage (inline or not) on some object or function which does the coun=
ting. This should also be mutex-locked, because each TU is allowed to be in=
itialized in a different thread.
If the counter values are assigned at compile time, you need to compute eac=
h value immediately as the counter updates are declared within the TU. Othe=
r TUs are invisible until linking. In order to have consistent counter valu=
es, the counting and assignment of the counter to whatever variables need t=
o be the same across all TUs that observe the counter or those variables.
There is no way to "peek" at things in another TU, because that information=
doesn't exist. Maybe the next TU hasn't even be written yet by a programme=
r. inline certainly won't help.
--=20
---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
--Apple-Mail=_48C506D6-33DF-4AC5-9FC9-9883D2C39E0D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;09–20, at 1:40 AM, Vittorio Romeo <<a href=3D"mailto:vittori=
o.romeo.vee@gmail.com">vittorio.romeo.vee@gmail.com</a>> wrote:</div><br=
class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div dir=3D"=
ltr"><font size=3D"1">On Friday, 19 September 2014 19:23:22 UTC+2, David Kr=
auss wrote:</font><blockquote class=3D"gmail_quote" style=3D"margin: 0px 0=
px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204)=
; border-left-style: solid; padding-left: 1ex; position: static; z-index: a=
uto;"><div style=3D"word-wrap:break-word"><font size=3D"1">I see some discu=
ssion of the ODR in this thread. Any counter usage with linkage does need t=
o be placed in the header file and repeated exactly the same in each TU. Av=
oid sharing a counter between headers unless those headers are all-or-nothi=
ng.</font></div></blockquote><div><br></div><div>Can you elaborate more on =
this? What I am currently doing is putting the workaround code in an header=
file (header-only library) and using inline in every function. Is that not=
guaranteed to return expected values in different TUs? (values that never =
repeat, I mean)</div></div></blockquote><br></div><div>It’s in refere=
nce to the StackOverflow answer.</div><div><br></div><div>If the counter va=
lues are assigned at runtime, it’s sufficient to have external linkag=
e (inline or not) on some object or function which does the counting. This =
should also be mutex-locked, because each TU is allowed to be initialized i=
n a different thread.</div><div><br></div><div>If the counter values are as=
signed at compile time, you need to compute each value immediately as the c=
ounter updates are declared within the TU. Other TUs are invisible until li=
nking. In order to have consistent counter values, the counting and assignm=
ent of the counter to whatever variables need to be the same across all TUs=
that observe the counter or those variables.</div><div><br></div>There is =
no way to “peek” at things in another TU, because that informat=
ion doesn’t exist. Maybe the next TU hasn’t even be written yet=
by a programmer. <font face=3D"Courier">inline</font> certainly won’=
t help.<div><br></div></body></html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--Apple-Mail=_48C506D6-33DF-4AC5-9FC9-9883D2C39E0D--
.
Author: Matheus Izvekov <mizvekov@gmail.com>
Date: Fri, 19 Sep 2014 23:18:59 -0700 (PDT)
Raw View
------=_Part_2969_1401361535.1411193939830
Content-Type: text/plain; charset=UTF-8
On Friday, September 19, 2014 3:49:07 PM UTC-3, Sean Middleditch wrote:
>
>
> That said... I don't think Vittorio's feature request is possible. There's
> no way to give stable compile-time IDs across translation units in the
> usage pattern he wants them in, even with a new magic language construct.
>
Yeah I see, it would require something like -flto to be mandated.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_2969_1401361535.1411193939830
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Friday, September 19, 2014 3:49:07 PM UTC-3, Sean Middl=
editch wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">=
<div></div><div><br></div><div>That said... I don't think Vittorio's featur=
e request is possible. There's no way to give stable compile-time IDs acros=
s translation units in the usage pattern he wants them in, even with a new =
magic language construct.</div></div></blockquote><div><br>Yeah I see, it w=
ould require something like -flto to be mandated.<br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_2969_1401361535.1411193939830--
.
Author: Thiago Macieira <thiago@macieira.org>
Date: Sat, 20 Sep 2014 11:58:37 -0700
Raw View
On Friday 19 September 2014 23:18:59 Matheus Izvekov wrote:
> Yeah I see, it would require something like -flto to be mandated.
With static linking. LTO is not enough if the same symbol were used in
different shared libraries. And even then, I'm depending on how each compiler
does link-time code generation, it may not work at all.
No, the standard requires that the functions be the same, with the same body,
or the program is ill-formed.
But you know what you could use for an identifier that is unique? The address
of the function itself.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Thiago Macieira <thiago@macieira.org>
Date: Sat, 20 Sep 2014 12:02:37 -0700
Raw View
On Saturday 20 September 2014 07:32:29 David Krauss wrote:
> If the counter values are assigned at runtime, it's sufficient to have
> external linkage (inline or not) on some object or function which does the
> counting. This should also be mutex-locked, because each TU is allowed to
> be initialized in a different thread.
std::atomic<int> can do it without the need for locking a mutex.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Matheus Izvekov <mizvekov@gmail.com>
Date: Sat, 20 Sep 2014 14:55:42 -0700 (PDT)
Raw View
------=_Part_3388_1370093898.1411250142147
Content-Type: text/plain; charset=UTF-8
On Saturday, September 20, 2014 3:58:44 PM UTC-3, Thiago Macieira wrote:
>
> But you know what you could use for an identifier that is unique? The
> address
> of the function itself.
>
>
That has a small caveat though: the compiler is allowed to merge different
functions
and use the same address for them in case their generated code is identical.
I have been bitten by this before :)
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_3388_1370093898.1411250142147
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Saturday, September 20, 2014 3:58:44 PM UTC-3, Thiago M=
acieira wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">But you know wha=
t you could use for an identifier that is unique? The address=20
<br>of the function itself.
<br><br></blockquote><div><br>That has a small caveat though: the compiler =
is allowed to merge different functions<br>and use the same address for the=
m in case their generated code is identical.<br>I have been bitten by this =
before :)<br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_3388_1370093898.1411250142147--
.
Author: Sean Middleditch <sean@middleditch.us>
Date: Sun, 21 Sep 2014 12:51:49 -0700
Raw View
On Sat, Sep 20, 2014 at 2:55 PM, Matheus Izvekov <mizvekov@gmail.com> wrote:
> On Saturday, September 20, 2014 3:58:44 PM UTC-3, Thiago Macieira wrote:
>>
>> But you know what you could use for an identifier that is unique? The
>> address
>> of the function itself.
>>
>
> That has a small caveat though: the compiler is allowed to merge different
> functions
I don't think so. I believe that the linker is not supposed to do that
for any distinct definition, be it a function or global or static;
addresses must be unique for each definition. At least one popular
implementation does do it by default but I'm fairly sure that's not
conforming behavior (and it can be turned off). Other implementations
allow the user to pass a flag that enables the behavior. Some even
allow a different flag that only merges identical functions when it
can be proved that their addresses are not taken.
--
Sean Middleditch
http://seanmiddleditch.com
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Matheus Izvekov <mizvekov@gmail.com>
Date: Sun, 21 Sep 2014 12:56:32 -0700 (PDT)
Raw View
------=_Part_3717_1785861649.1411329392077
Content-Type: text/plain; charset=UTF-8
On Sunday, September 21, 2014 4:51:52 PM UTC-3, Sean Middleditch wrote:
>
> I don't think so. I believe that the linker is not supposed to do that
> for any distinct definition, be it a function or global or static;
> addresses must be unique for each definition. At least one popular
> implementation does do it by default but I'm fairly sure that's not
> conforming behavior (and it can be turned off).
That might be the case, yeah, I am fairly certain visual studio 2013 does
this, and there is
nothing in the option that turns this on/off indicating this is not
standard conforming.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_3717_1785861649.1411329392077
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Sunday, September 21, 2014 4:51:52 PM UTC-3, Sean Middl=
editch wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">I don't think so.=
I believe that the linker is not supposed to do that
<br>for any distinct definition, be it a function or global or static;
<br>addresses must be unique for each definition. At least one popular
<br>implementation does do it by default but I'm fairly sure that's not
<br>conforming behavior (and it can be turned off).</blockquote><div><br>Th=
at might be the case, yeah, I am fairly certain visual studio 2013 does thi=
s, and there is<br>nothing in the option that turns this on/off indicating =
this is not standard conforming.<br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_3717_1785861649.1411329392077--
.
Author: Sean Middleditch <sean@middleditch.us>
Date: Sun, 21 Sep 2014 13:12:59 -0700
Raw View
On Sun, Sep 21, 2014 at 12:56 PM, Matheus Izvekov <mizvekov@gmail.com> wrote:
> On Sunday, September 21, 2014 4:51:52 PM UTC-3, Sean Middleditch wrote:
>>
>> I don't think so. I believe that the linker is not supposed to do that
>> for any distinct definition, be it a function or global or static;
>> addresses must be unique for each definition. At least one popular
>> implementation does do it by default but I'm fairly sure that's not
>> conforming behavior (and it can be turned off).
>
>
> That might be the case, yeah, I am fairly certain visual studio 2013 does
> this, and there is
> nothing in the option that turns this on/off indicating this is not standard
> conforming.
It's called Identical COMDAT Folding in their implementation. See
http://msdn.microsoft.com/en-us/library/bxwfs976.aspx for information
on controlling this behavior.
--
Sean Middleditch
http://seanmiddleditch.com
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Matheus Izvekov <mizvekov@gmail.com>
Date: Sun, 21 Sep 2014 13:17:39 -0700 (PDT)
Raw View
------=_Part_53_1023557583.1411330659675
Content-Type: text/plain; charset=UTF-8
On Sunday, September 21, 2014 5:13:01 PM UTC-3, Sean Middleditch wrote:
>
> It's called Identical COMDAT Folding in their implementation. See
> http://msdn.microsoft.com/en-us/library/bxwfs976.aspx for information
> on controlling this behavior.
>
>
Yeah exactly, and notice how it seems there is no mention of this
optimization being non-conforming even in that documentation you linked.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_53_1023557583.1411330659675
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Sunday, September 21, 2014 5:13:01 PM UTC-3, Sean Middl=
editch wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">It's called Ident=
ical COMDAT Folding in their implementation. See
<br><a href=3D"http://msdn.microsoft.com/en-us/library/bxwfs976.aspx" targe=
t=3D"_blank" onmousedown=3D"this.href=3D'http://www.google.com/url?q\75http=
%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fbxwfs976.aspx\46sa\75D\46sn=
tz\0751\46usg\75AFQjCNFzG8XIuIumNA0Kbg3CJTCWugsKKw';return true;" onclick=
=3D"this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fmsdn.microsoft.=
com%2Fen-us%2Flibrary%2Fbxwfs976.aspx\46sa\75D\46sntz\0751\46usg\75AFQjCNFz=
G8XIuIumNA0Kbg3CJTCWugsKKw';return true;">http://msdn.microsoft.com/en-<wbr=
>us/library/bxwfs976.aspx</a> for information
<br>on controlling this behavior.
<br>
<br></blockquote><div><br>Yeah exactly, and notice how it seems there is no=
mention of this optimization being non-conforming even in that documentati=
on you linked. <br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_53_1023557583.1411330659675--
.
Author: Thiago Macieira <thiago@macieira.org>
Date: Sun, 21 Sep 2014 13:31:06 -0700
Raw View
On Sunday 21 September 2014 13:17:39 Matheus Izvekov wrote:
> > http://msdn.microsoft.com/en-us/library/bxwfs976.aspx for information
> > on controlling this behavior.
>
> Yeah exactly, and notice how it seems there is no mention of this
> optimization being non-conforming even in that documentation you linked.
MSVC and DLLs have plenty of non-conforming behaviours, including violation of
ODR rules and over-instantiation of templates.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: David Krauss <potswa@gmail.com>
Date: Mon, 22 Sep 2014 11:19:26 +0800
Raw View
On 2014-09-22, at 4:12 AM, Sean Middleditch <sean@middleditch.us> wrote:
> It's called Identical COMDAT Folding in their implementation. See
> http://msdn.microsoft.com/en-us/library/bxwfs976.aspx for information
> on controlling this behavior.
Yuck. I'd heard of MSVC doing that, but I assumed they also generated trampoline instructions (jumps or prologue nops) to generate unique addresses when observation happens.
To be sure, this optimization doesn't need to be nonconforming. There should be no difficulty to proving that an address is never taken, just generate a linker symbol for each trampoline and let it get stripped by the normal process.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Sean Middleditch <sean@middleditch.us>
Date: Sun, 21 Sep 2014 20:47:32 -0700
Raw View
On Sun, Sep 21, 2014 at 8:19 PM, David Krauss <potswa@gmail.com> wrote:
> To be sure, this optimization doesn't need to be nonconforming. There should be no difficulty to proving that an address is never taken, just generate a linker symbol for each trampoline and let it get stripped by the normal process.
See --icf=safe in Gold for an implementation of ICF that is conforming.
--
Sean Middleditch
http://seanmiddleditch.com
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Matthew Fioravante <fmatthew5876@gmail.com>
Date: Mon, 22 Sep 2014 06:54:08 -0700 (PDT)
Raw View
------=_Part_434_940439499.1411394048716
Content-Type: text/plain; charset=UTF-8
I had another use case for a compile time counter.
I created a instruction set (think assembly) to model a certain system. I
had a raw instruction class which was essentially just an integer ordinal
and an untyped buffer fixed size to store the operands. I had a different
type for each different kind of instruction and set the ordinal of that
instruction to a compile time integer value. I could convert the specific
instruction types to and from the raw instruction type, allowing an
efficient type erasure scheme. The idea behind the API was to allow callers
to push any sequence of instructions into a buffer and then send them off
to be executed by the system.
In my case I just assigned the integer values to each class manually using
templates and inheritance, taking care not to have them collide. It would
have been nice if there was compiler magic to do this for me.
As others have mentioned, such a feature may not be possible. There is a
chicken and egg problem here. For a particular TU, the compiler needs to
know the actual value of the counter in order to compile the TU and this
value cannot be known until link time. Throw shared libraries into the mix
and you've got even more complications.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_434_940439499.1411394048716
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I had another use case for a compile time counter. <br><br=
>I created a instruction set (think assembly) to model a certain system. I =
had a raw instruction class which was essentially just an integer ordinal a=
nd an untyped buffer fixed size to store the operands. I had a different ty=
pe for each different kind of instruction and set the ordinal of that instr=
uction to a compile time integer value. I could convert the specific instru=
ction types to and from the raw instruction type, allowing an efficient typ=
e erasure scheme. The idea behind the API was to allow callers to push any =
sequence of instructions into a buffer and then send them off to be execute=
d by the system.<br><br>In my case I just assigned the integer values to ea=
ch class manually using templates and inheritance, taking care not to have =
them collide. It would have been nice if there was compiler magic to do thi=
s for me.<br><br>As others have mentioned, such a feature may not be possib=
le. There is a chicken and egg problem here. For a particular TU, the compi=
ler needs to know the actual value of the counter in order to compile the T=
U and this value cannot be known until link time. Throw shared libraries in=
to the mix and you've got even more complications.<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_434_940439499.1411394048716--
.