Topic: Auto constness.
Author: sasho648 <sasho648@mail.bg>
Date: Tue, 13 Jan 2015 00:43:46 -0800 (PST)
Raw View
------=_Part_17_2043813442.1421138626072
Content-Type: multipart/alternative;
boundary="----=_Part_18_1371585929.1421138626073"
------=_Part_18_1371585929.1421138626073
Content-Type: text/plain; charset=UTF-8
I think introducing something like auto type qualifier would be useful.
Here are some examples when.
1. Casting to another type while preserving constness:
Imagine you have one variable 'a' of type 'int'. You cast and use it
somewhere in your code as reference to 'float'. After some-time you decide
that you want this variable to be constant but this could create problems
as the reference cast wouldn't preserve it!
Examples:
void Func(float &);
void Func(const float &);
int main()
{
int a; // if this becomes constant the below call wouldn't change and will
still instance 'void Func(float &)' thus creating potentinal problems
Func((float &) a);
}
2. Creating type-qualifier general functions.
One example of the above
<http://stackoverflow.com/questions/27083004/universal-member-function-definition-instanceable-from-both-const-non-cons>
..
Here is what I purpose (something like):
*auto_const*
*auto_**volatile*
So we could write genereal type-qualifier functions like:
void Func1(auto_const float &arg)
{
Func(arg); //depending of 'arg' constness, will instance either 'void
Func(const float &)' or 'void Func(float &)'
}
And fix the first example as follows:
void Func(float &);
void Func(const float &);
int main()
{
int a;
Func((auto_const float &) a); //depending of a constness it will be cast
to either 'const float &' ot 'float &'
}
--
---
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_18_1371585929.1421138626073
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I think introducing something like auto type qualifier wou=
ld be useful. Here are some examples when.<br><br>1. Casting to another typ=
e while preserving constness:<br><div><br></div><div>Imagine you have one v=
ariable 'a' of type 'int'. You cast and use it somewhere in your code as re=
ference to 'float'. After some-time you decide that you want this variable =
to be constant but this could create problems as the reference cast wouldn'=
t preserve it!<br><br>Examples:<br><br></div><div class=3D"prettyprint" sty=
le=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgrou=
nd-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"su=
bprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">voi=
d</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #606;" class=3D"styled-by-prettify">Func</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">float</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&);</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br><br><br></span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">void</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-b=
y-prettify">Func</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(</span><span style=3D"color: #008;" class=3D"styled-by-prettify">con=
st</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">float</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"co=
lor: #000;" class=3D"styled-by-prettify"><br><br><br></span><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"styled-by-prettify">()</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br></span><span style=3D"color: rgb(0, 0, 0);"><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span></span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> a</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by-=
prettify">// if this becomes constant the below call wouldn't change and wi=
ll still instance 'void Func(float &)' thus creating potentinal problem=
s</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br><=
br> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">F=
unc</span><span style=3D"color: #660;" class=3D"styled-by-prettify">((</spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">float</span><sp=
an 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"> a</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br><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></span></div></code></div><div><br><br></div><div>2. Cr=
eating type-qualifier general functions.<br></div><div><br></div><div>One e=
xample of the <a href=3D"http://stackoverflow.com/questions/27083004/univer=
sal-member-function-definition-instanceable-from-both-const-non-cons">above=
</a>.</div><div><br></div><div>Here is what I purpose (something like):<br>=
<br><b>auto_const</b></div><div><b><br></b></div><div><b>auto_</b><span sty=
le=3D"color: rgb(0, 0, 0); font-family: DejaVuSans, 'DejaVu Sans', arial, s=
ans-serif; font-size: 12.8000001907349px; line-height: 15.3600006103516px;"=
><b>volatile</b></span></div><div><span style=3D"color: rgb(0, 0, 0); font-=
family: DejaVuSans, 'DejaVu Sans', arial, sans-serif; font-size: 12.8000001=
907349px; line-height: 15.3600006103516px;"><b><br></b></span></div><div>So=
we could write genereal type-qualifier functions like:</div><div><br></div=
><div class=3D"prettyprint" 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: #008;"=
class=3D"styled-by-prettify">void</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"style=
d-by-prettify">Func1</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>auto_const </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">float</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">arg</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br> </span><span style=3D"color: #60=
6;" class=3D"styled-by-prettify">Func</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">arg</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #800;" class=3D"styled-by-prettify">//depe=
nding of 'arg' constness, will instance either 'void Func(const float &=
)' or 'void Func(float &)'</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">}</span></div></code></div><div><br></div><div><br></div><div=
>And fix the first example as follows:<br><br><span class=3D"styled-by-pret=
tify" style=3D"font-family: monospace; color: rgb(0, 0, 136); background-co=
lor: rgb(250, 250, 250);"></span></div><div class=3D"prettyprint" style=3D"=
border: 1px solid rgb(187, 187, 187); word-wrap: break-word; background-col=
or: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprett=
yprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">void</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #606;" class=3D"styled-by-prettify">Func</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">float</span><span style=3D"color: #00=
0;" 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><br><br></span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">void</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-p=
rettify">Func</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">(</span><span style=3D"color: #008;" class=3D"styled-by-prettify">const<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">float</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">&);</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br><br><br></span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> main</span><span style=3D"color: #660;" c=
lass=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><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br> </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">int</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> a=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br><br><br> <=
/span><span style=3D"color: #606;" class=3D"styled-by-prettify">Func</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">((</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">auto_const </span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">float</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: #0=
00;" class=3D"styled-by-prettify"> a</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by-p=
rettify">//depending of a constness it will be cast to either 'const float =
&' ot 'float &'</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"><br><br></span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">}</span></div></code></div><div><span class=3D"styled-by-prettif=
y" style=3D"font-family: monospace; color: rgb(102, 102, 0); background-col=
or: rgb(250, 250, 250);"><br></span><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_18_1371585929.1421138626073--
------=_Part_17_2043813442.1421138626072--
.
Author: Douglas Boffey <douglas.boffey@gmail.com>
Date: Tue, 13 Jan 2015 08:51:08 +0000
Raw View
Why would you want a function that sometimes takes const and sometimes not?
On 1/13/15, sasho648 <sasho648@mail.bg> wrote:
> I think introducing something like auto type qualifier would be useful.
> Here are some examples when.
>
> 1. Casting to another type while preserving constness:
>
> Imagine you have one variable 'a' of type 'int'. You cast and use it
> somewhere in your code as reference to 'float'. After some-time you decide
> that you want this variable to be constant but this could create problems
> as the reference cast wouldn't preserve it!
>
> Examples:
>
> void Func(float &);
>
>
> void Func(const float &);
>
>
> int main()
> {
> int a; // if this becomes constant the below call wouldn't change and will
>
> still instance 'void Func(float &)' thus creating potentinal problems
>
>
> Func((float &) a);
>
> }
>
>
> 2. Creating type-qualifier general functions.
>
> One example of the above
> <http://stackoverflow.com/questions/27083004/universal-member-function-definition-instanceable-from-both-const-non-cons>
> .
>
> Here is what I purpose (something like):
>
> *auto_const*
>
> *auto_**volatile*
>
> So we could write genereal type-qualifier functions like:
>
> void Func1(auto_const float &arg)
> {
> Func(arg); //depending of 'arg' constness, will instance either 'void
> Func(const float &)' or 'void Func(float &)'
> }
>
>
> And fix the first example as follows:
>
> void Func(float &);
>
>
> void Func(const float &);
>
>
> int main()
> {
> int a;
>
>
> Func((auto_const float &) a); //depending of a constness it will be cast
> to either 'const float &' ot 'float &'
>
> }
>
>
> --
>
> ---
> 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/.
>
--
---
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: sasho648 <sasho648@mail.bg>
Date: Wed, 14 Jan 2015 11:31:50 -0800 (PST)
Raw View
------=_Part_3132_786346156.1421263910072
Content-Type: multipart/alternative;
boundary="----=_Part_3133_505480254.1421263910072"
------=_Part_3133_505480254.1421263910072
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
The same reason any other general functions are written. I had given an=20
link to stackoverflow with example of my own experience. I'll re-write it=
=20
here to illustrate better.
I was writing a 'List' container of my own because I was writing some=20
low-level app in which I wanted to have a view of every single piece of=20
code and thus not use external libraries. Anyway I decide to add a method=
=20
'ExecuteOnEachElement' which will call some lambda function passed as=20
parameter with each of it's elements. In this case I don't care are the=20
elements of my list constant or not - this is important only for the lambda=
=20
function passed. Now you'll probably say - write a 'const' method then - it=
=20
will be instanceable from both const & non-const objects but here goes the=
=20
tricky part - 'this' pointer will be always constant and the lambda=20
function instanced should always takes a constant argument which is not=20
what we want. Here is code snippet showing this:
=20
template <typename T>
struct sList
{
sList(initializer_list<T>);
inline void DoForEachList(auto pFunc) const
{
for(auto p =3D this; p; p =3D p->pNext)
pFunc(p); //here if 'decltype(pFunc)' is 'void=20
Func(sList<T>*)' error will occur
}
~sList();
T dat;
sList *pNext =3D nullptr;
};
The only way to implement wanted behavior for now is using an static=20
function with deduced first parameter and write both 'const' and normal=20
method instancing it, because we can't left the type of 'this' deduced=20
(partially or at any way).
template <typename T>
struct sList
{
void DoForEachList(auto pFunc)
{
DoForEachListHelper(*this, pFunc);
}
void DoForEachList(auto pFunc) const
{
DoForEachListHelper(*this, pFunc);
}
private:
static void DoForEachListHelper(auto&& self, auto pFunc)
{
for(auto p =3D &self; p; p =3D pNext->pNext)
pFunc(p);
}
};
If we introduce partial type deduction by only specifying type (and not CV=
=20
specifier) we could write the above as a general method for either 'const'=
=20
or normal 'this' pointer like this:
=20
template <typename T>
struct sList
{
sList(initializer_list<T>);
inline void DoForEachList(auto pFunc) auto_const //here 'this'=
=20
will be either of typr 'const sList<T>' or 'sList<T>'
{
for(auto p =3D this; p; p =3D p->pNext)
pFunc(p); //here 'p' is either 'const sList<T>' or=20
'sList<T>'
}
~sList();
T dat;
sList *pNext =3D nullptr;
};
Which definetly safes us writting additional code and functions which is=20
something.
This is an example for using the auto constness in a class method but it=20
could be actually used in normal functions too. Some examples:
void foo1(int &arg);
void foo1(const int &arg);
void foo1(const double &arg);
void foo(auto_const int &arg)
{
foo1(arg);
//uses 'arg' in a way that it doesn't depends on it's 'int' type
//example:
void WriteFile(void *); //declared in some additional header file =20
WriteFile(&arg);
}
int main()
{
int a;
const int b;
double c;
foo(a); //will instance with parameter of type 'int'
foo(b); //will instance with parameter of type 'const int'
foo(c); //error 'c' doesn't match type pattern ('int')
}
The above can't be written using full type deduction because in such case=
=20
argument of type 'double' will be valid and this is unwanted.
But the main usage of it will be in 'const-safe' casting. This is when you=
=20
want to cast into certain other type but without lossing possible=20
constness. Examples:
int main()
{
void *pa;
unsigned long dwCfgFile =3D *((unsigned long *&) pCurrent)++; //here=20
'pCurrent' will be always modified no-matter it's cv
}
In the above example if you decide to make 'pa' constant you'll be=20
surprised when it gets edited by the second line. Of-course in real-life=20
this can be some way more complicated and locating this error hard. If we=
=20
introduce 'cv' independant cast by using auto constness this line will=20
report error if 'pa' becames constant. Example:
int main()
{
void *pa;
unsigned long dwCfgFile =3D *((unsigned long *auto_const &) pCurrent)++=
; //here=20
'pCurrent' cast target type cv will be deduced from the variable casted
}
Now if we change 'pa' type cv to constant the second line will report error=
:
int main()
{
void *const pa;
unsigned long dwCfgFile =3D *((unsigned long *auto_const &) pCurrent)++=
; //evaluated=20
to as:
//unsigned long dwCfgFile =3D *((unsigned long *const &) pCurrent)++;=
=20
//thus reporting error
}
Which is useful - at least for me when working with data pointers.
=D0=B2=D1=82=D0=BE=D1=80=D0=BD=D0=B8=D0=BA, 13 =D1=8F=D0=BD=D1=83=D0=B0=D1=
=80=D0=B8 2015 =D0=B3., 10:51:10 UTC+2, Douglas Boffey =D0=BD=D0=B0=D0=BF=
=D0=B8=D1=81=D0=B0:
>
> Why would you want a function that sometimes takes const and sometimes=20
> not?=20
>
> On 1/13/15, sasho648 <sash...@mail.bg <javascript:>> wrote:=20
> > I think introducing something like auto type qualifier would be useful.=
=20
> > Here are some examples when.=20
> >=20
> > 1. Casting to another type while preserving constness:=20
> >=20
> > Imagine you have one variable 'a' of type 'int'. You cast and use it=20
> > somewhere in your code as reference to 'float'. After some-time you=20
> decide=20
> > that you want this variable to be constant but this could create=20
> problems=20
> > as the reference cast wouldn't preserve it!=20
> >=20
> > Examples:=20
> >=20
> > void Func(float &);=20
> >=20
> >=20
> > void Func(const float &);=20
> >=20
> >=20
> > int main()=20
> > {=20
> > int a; // if this becomes constant the below call wouldn't change and=
=20
> will=20
> >=20
> > still instance 'void Func(float &)' thus creating potentinal problems=
=20
> >=20
> >=20
> > Func((float &) a);=20
> >=20
> > }=20
> >=20
> >=20
> > 2. Creating type-qualifier general functions.=20
> >=20
> > One example of the above=20
> > <
> http://stackoverflow.com/questions/27083004/universal-member-function-def=
inition-instanceable-from-both-const-non-cons>=20
>
> > .=20
> >=20
> > Here is what I purpose (something like):=20
> >=20
> > *auto_const*=20
> >=20
> > *auto_**volatile*=20
> >=20
> > So we could write genereal type-qualifier functions like:=20
> >=20
> > void Func1(auto_const float &arg)=20
> > {=20
> > Func(arg); //depending of 'arg' constness, will instance either 'void=
=20
> > Func(const float &)' or 'void Func(float &)'=20
> > }=20
> >=20
> >=20
> > And fix the first example as follows:=20
> >=20
> > void Func(float &);=20
> >=20
> >=20
> > void Func(const float &);=20
> >=20
> >=20
> > int main()=20
> > {=20
> > int a;=20
> >=20
> >=20
> > Func((auto_const float &) a); //depending of a constness it will be=20
> cast=20
> > to either 'const float &' ot 'float &'=20
> >=20
> > }=20
> >=20
> >=20
> > --=20
> >=20
> > ---=20
> > You received this message because you are subscribed to the Google=20
> Groups=20
> > "ISO C++ Standard - Future Proposals" group.=20
> > To unsubscribe from this group and stop receiving emails from it, send=
=20
> an=20
> > email to std-proposal...@isocpp.org <javascript:>.=20
> > To post to this group, send email to std-pr...@isocpp.org <javascript:>=
..=20
>
> > Visit this group at=20
> > http://groups.google.com/a/isocpp.org/group/std-proposals/.=20
> >=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/.
------=_Part_3133_505480254.1421263910072
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">The same reason any other general functions are written. I=
had given an link to stackoverflow with example of my own experience. I'll=
re-write it here to illustrate better.<div><br></div><div>I was writing a =
'List' container of my own because I was writing some low-level app in whic=
h I wanted to have a view of every single piece of code and thus not use ex=
ternal libraries. Anyway I decide to add a method 'ExecuteOnEachElement' wh=
ich will call some lambda function passed as parameter with each of it's el=
ements. In this case I don't care are the elements of my list constant or n=
ot - this is important only for the lambda function passed. Now you'll prob=
ably say - write a 'const' method then - it will be instanceable from both =
const & non-const objects but here goes the tricky part - 'this' pointe=
r will be always constant and the lambda function instanced should always t=
akes a constant argument which is not what we want. Here is code snippet sh=
owing this:<br><br><div> </div><div class=3D"pre=
ttyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-w=
ord; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><di=
v class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #008;" c=
lass=3D"styled-by-prettify">template</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify"><</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> T</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>></span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&=
nbsp; </span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">struct</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> sList<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> &nbs=
p; sList</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">initializer=
_list</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">T</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">>);</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br><br> &n=
bsp; </span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">inline</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">void</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #606;" class=3D"styled-by-prettify">DoForEachLis=
t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> pFunc</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: #008;" =
class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br> </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><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">for</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">auto</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> p </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">this</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> p</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> p </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">-></span><span style=3D"color: #000;" class=3D"styled-by-prettify">pNe=
xt</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
pFunc</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">p</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"sty=
led-by-prettify">//here if 'decltype(pFunc)' is 'void Func(sList<T>*)=
' error will occur</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br> </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br><br> &nbs=
p; </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
~</span><span style=3D"color: #000;" class=3D"styled-by-prettify">sList</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">();</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br><br> =
T dat</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br><br> sList </sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">*</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">pNext </span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">nullptr</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"c=
olor: #660;" class=3D"styled-by-prettify">};</span></div></code></div><div>=
<br>The only way to implement wanted behavior for now is using an static fu=
nction with deduced first parameter and write both 'const' and normal metho=
d instancing it, because we can't left the type of 'this' deduced (partiall=
y or at any way).</div><div><br></div><div><div class=3D"prettyprint" 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"subp=
rettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">templ=
ate</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: #008;" class=3D"styled-by-prettify">typename</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> T</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;" cl=
ass=3D"styled-by-prettify"> sList<br></span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br> </span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">void</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by=
-prettify">DoForEachList</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
pFunc</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nbs=
p; </span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">DoForEachListHelper</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(*</span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">this</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> pFunc<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">void</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify">DoForEachList</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> pFunc</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: #008;" class=3D"style=
d-by-prettify">const</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br> </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br> </span><span style=3D"color: #606;=
" class=3D"styled-by-prettify">DoForEachListHelper</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">(*</span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">this</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> pFunc</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br> </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">pr=
ivate</span><span style=3D"color: #660;" class=3D"styled-by-prettify">:</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">static=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">void</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #606;" class=3D"styled-by-prettify">DoForEachListHelper</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">auto</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: #008;" cla=
ss=3D"styled-by-prettify">self</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: #008;" class=3D"styled-by-prettify=
">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> pFu=
nc</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><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">for</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> p </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&</span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">self</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> p=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> pNext</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">-></span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">pNext</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>  =
; pFunc</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">p</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"><br> </sp=
an><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></div></code></div>=
<div><br></div></div><div>If we introduce partial type deduction by only sp=
ecifying type (and not CV specifier) we could write the above as a general =
method for either 'const' or normal 'this' pointer like this:<br><br><span =
class=3D"styled-by-prettify" style=3D"font-family: monospace; color: rgb(0,=
0, 136); background-color: rgb(250, 250, 250);">  =
; </span><div class=3D"prettyprint" 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: =
#000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">template</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:=
#008;" class=3D"styled-by-prettify">typename</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" cl=
ass=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"colo=
r: #000;" class=3D"styled-by-prettify"> sList<br>  =
; </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
sList</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">initializer_list</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">>);</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br><br> </span><span style=3D"c=
olor: #008;" 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">void</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"style=
d-by-prettify">DoForEachList</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> pFunc</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> auto_cons=
t </span><span style=3D"color: #800;" class=3D"styled-by-prettify">//here '=
this' will be either of typr 'const sList<T>' or 'sList<T>'</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br> =
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">for</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> p </span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">this</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> p </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> p</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">-></span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">pNext</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br>  =
; pFunc</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">p</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">//here 'p' is eit=
her </span><span style=3D"color: rgb(136, 0, 0);"><span style=3D"color: #80=
0;" class=3D"styled-by-prettify">'const sList<T>' or 'sList<T>'=
</span></span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
> </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br><br> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">~</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">sList</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">();</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"><br><br> &nb=
sp; T dat</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br><br> sList </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">*</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">pNext </span><span style=3D"color:=
#660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">nullptr</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br> </span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">};</span></div></code></div><span class=3D"s=
tyled-by-prettify" style=3D"font-family: monospace; color: rgb(102, 102, 0)=
; background-color: rgb(250, 250, 250);"><br></span>Which definetly safes u=
s writting additional code and functions which is something.</div><div><br>=
</div><div>This is an example for using the auto constness in a class metho=
d but it could be actually used in normal functions too. Some examples:</di=
v><div><br></div><div class=3D"prettyprint" 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: #008;" class=3D"styled-by-prettify">void</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> foo1</span><span style=3D"color: #66=
0;" 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"sty=
led-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">&</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">arg</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">void</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> foo1</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">int</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-pret=
tify">arg</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br><=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">void</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> foo1</span><span=
style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">const</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;"=
class=3D"styled-by-prettify">double</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify">arg</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">void<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> foo</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">auto_const </span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">arg</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br> foo1</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">arg</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br><br><br> </=
span><span style=3D"color: #800;" class=3D"styled-by-prettify">//uses 'arg'=
in a way that it doesn't depends on it's 'int' type</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br><br><br> </span><span st=
yle=3D"color: #800;" class=3D"styled-by-prettify">//example:</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br><br><br> </span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">void</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #606;" class=3D"styled-by-prettify">WriteFile</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">void</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">*);</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by-prettify=
">//declared in some additional header file </span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br><br><br><br> </span><span =
style=3D"color: #606;" class=3D"styled-by-prettify">WriteFile</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(&</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">arg</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;" cla=
ss=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br><br><br></span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> main</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">()</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br></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">int</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"><br><br> </span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> b</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br><br> </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">double</span><span style=3D"color: #000;" class=3D"styled-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"><br><br> fo=
o</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 styl=
e=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #800;=
" class=3D"styled-by-prettify">//will instance with parameter of type 'int'=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>&n=
bsp;foo</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">b</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=
: #800;" class=3D"styled-by-prettify">//will instance with parameter of typ=
e 'const int'</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br><br> foo</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #000;" class=3D"styled-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"> </span><span st=
yle=3D"color: #800;" class=3D"styled-by-prettify">//error 'c' doesn't match=
type pattern ('int')</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></div></code></div><div><br></div><div>The above can't be writ=
ten using full type deduction because in such case argument of type 'double=
' will be valid and this is unwanted.<br><br>But the main usage of it will =
be in 'const-safe' casting. This is when you want to cast into certain othe=
r type but without lossing possible constness. Examples:<br><br></div><div =
class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-w=
rap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"pret=
typrint"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> main</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br></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">voi=
d</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">*</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">pa</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br><br> </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">unsigned</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">long</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> dwCfgFile </span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">=3D</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: #008;" class=3D"styled-by-pretti=
fy">unsigned</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">long</s=
pan><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"> pCurrent</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">)++;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #8=
00;" class=3D"styled-by-prettify">//here 'pCurrent' will be always modified=
no-matter it's cv</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">}</span></div></code></div><div><br></div><div>In the above example if yo=
u decide to make 'pa' constant you'll be surprised when it gets edited by t=
he second line. Of-course in real-life this can be some way more complicate=
d and locating this error hard. If we introduce 'cv' independant cast by us=
ing auto constness this line will report error if 'pa' becames constant. Ex=
ample:<br><br><div class=3D"prettyprint" 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"co=
lor: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> main</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><span style=3D"color: #008;" class=3D"styled=
-by-prettify">void</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">*=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">pa</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br><br> </spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">unsigned</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">long</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> dwCfgFile </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">*((</span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">unsigned</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">long </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">*</span><span style=3D"color: rgb(0, 0, 0); font-family: Arial, H=
elvetica, sans-serif;">auto_const</span><span style=3D"color: rgb(0, 0, 0);=
font-family: Arial, Helvetica, sans-serif;"> </span><span class=3D"st=
yled-by-prettify" style=3D"font-family: Arial, Helvetica, sans-serif; color=
: rgb(102, 102, 0);">&)</span><span class=3D"styled-by-prettify" style=
=3D"font-family: Arial, Helvetica, sans-serif; color: rgb(0, 0, 0);"> pCurr=
ent</span><span class=3D"styled-by-prettify" style=3D"font-family: Arial, H=
elvetica, sans-serif; color: rgb(102, 102, 0);">)++;</span><span class=3D"s=
tyled-by-prettify" style=3D"font-family: Arial, Helvetica, sans-serif; colo=
r: rgb(0, 0, 0);"> </span><span class=3D"styled-by-prettify" style=3D"font-=
family: Arial, Helvetica, sans-serif; color: rgb(136, 0, 0);">//here 'pCurr=
ent' cast target type cv will be deduced from the variable casted</span></d=
iv><div class=3D"subprettyprint"><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">}</span></div></code></div><span class=3D"styled-by-prettif=
y" style=3D"font-family: monospace; color: rgb(102, 102, 0); background-col=
or: rgb(250, 250, 250);"><br></span></div>Now if we change 'pa' type cv to =
constant the second line will report error:<br><br><span class=3D"styled-by=
-prettify" style=3D"font-family: monospace; color: rgb(0, 0, 136); backgrou=
nd-color: rgb(250, 250, 250);"></span></div><div class=3D"prettyprint" styl=
e=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgroun=
d-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"sub=
prettyprint"><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"styled-by-prettify">()</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br></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">void</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">*</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">const</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> pa</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br><br> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">unsigned</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">long</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> d=
wCfgFile </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
=3D</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: #008;" class=3D"styled-by-prettify">unsigned</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">long </span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">*</span><span style=3D"color: rgb(=
0, 0, 0); font-family: Arial, Helvetica, sans-serif;">auto_const</span><spa=
n style=3D"color: rgb(0, 0, 0); font-family: Arial, Helvetica, sans-serif;"=
> </span><span class=3D"styled-by-prettify" style=3D"font-family: Aria=
l, Helvetica, sans-serif; color: rgb(102, 102, 0);">&)</span><span clas=
s=3D"styled-by-prettify" style=3D"font-family: Arial, Helvetica, sans-serif=
; color: rgb(0, 0, 0);"> pCurrent</span><span class=3D"styled-by-prettify" =
style=3D"font-family: Arial, Helvetica, sans-serif; color: rgb(102, 102, 0)=
;">)++;</span><span class=3D"styled-by-prettify" style=3D"font-family: Aria=
l, Helvetica, sans-serif; color: rgb(0, 0, 0);"> </span><span class=3D"styl=
ed-by-prettify" style=3D"font-family: Arial, Helvetica, sans-serif; color: =
rgb(136, 0, 0);">//evaluated to as:</span></div><div class=3D"subprettyprin=
t"><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; </span><span style=3D"color: #800;" class=3D"styled-by-prettify">//unsi=
gned long dwCfgFile =3D *((unsigned long *const &) pCurrent)++; //thus =
reporting error</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}=
</span></div></code></div><div><span class=3D"styled-by-prettify" style=3D"=
font-family: monospace; color: rgb(102, 102, 0); background-color: rgb(250,=
250, 250);"><br></span>Which is useful - at least for me when working with=
data pointers.<br><br>=D0=B2=D1=82=D0=BE=D1=80=D0=BD=D0=B8=D0=BA, 13 =D1=
=8F=D0=BD=D1=83=D0=B0=D1=80=D0=B8 2015 =D0=B3., 10:51:10 UTC+2, Douglas Bof=
fey =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0:<blockquote class=3D"gmail_quote" =
style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-l=
eft: 1ex;">Why would you want a function that sometimes takes const and som=
etimes not?
<br>
<br>On 1/13/15, sasho648 <<a href=3D"javascript:" target=3D"_blank" gdf-=
obfuscated-mailto=3D"67oeufJCI2EJ" onmousedown=3D"this.href=3D'javascript:'=
;return true;" onclick=3D"this.href=3D'javascript:';return true;">sash...@m=
ail.bg</a>> wrote:
<br>> I think introducing something like auto type qualifier would be us=
eful.
<br>> Here are some examples when.
<br>>
<br>> 1. Casting to another type while preserving constness:
<br>>
<br>> Imagine you have one variable 'a' of type 'int'. You cast and use =
it
<br>> somewhere in your code as reference to 'float'. After some-time yo=
u decide
<br>> that you want this variable to be constant but this could create p=
roblems
<br>> as the reference cast wouldn't preserve it!
<br>>
<br>> Examples:
<br>>
<br>> void Func(float &);
<br>>
<br>>
<br>> void Func(const float &);
<br>>
<br>>
<br>> int main()
<br>> {
<br>> int a; // if this becomes constant the below call wouldn't c=
hange and will
<br>>
<br>> still instance 'void Func(float &)' thus creating potentinal p=
roblems
<br>>
<br>>
<br>> Func((float &) a);
<br>>
<br>> }
<br>>
<br>>
<br>> 2. Creating type-qualifier general functions.
<br>>
<br>> One example of the above
<br>> <<a href=3D"http://stackoverflow.com/questions/27083004/univers=
al-member-function-definition-instanceable-from-both-const-non-cons" target=
=3D"_blank" onmousedown=3D"this.href=3D'http://www.google.com/url?q\75http%=
3A%2F%2Fstackoverflow.com%2Fquestions%2F27083004%2Funiversal-member-functio=
n-definition-instanceable-from-both-const-non-cons\46sa\75D\46sntz\0751\46u=
sg\75AFQjCNEr2rplfF4eBtvi0itgNFA_eZr1BA';return true;" onclick=3D"this.href=
=3D'http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestion=
s%2F27083004%2Funiversal-member-function-definition-instanceable-from-both-=
const-non-cons\46sa\75D\46sntz\0751\46usg\75AFQjCNEr2rplfF4eBtvi0itgNFA_eZr=
1BA';return true;">http://stackoverflow.com/<wbr>questions/27083004/univers=
al-<wbr>member-function-definition-<wbr>instanceable-from-both-const-<wbr>n=
on-cons</a>>
<br>> .
<br>>
<br>> Here is what I purpose (something like):
<br>>
<br>> *auto_const*
<br>>
<br>> *auto_**volatile*
<br>>
<br>> So we could write genereal type-qualifier functions like:
<br>>
<br>> void Func1(auto_const float &arg)
<br>> {
<br>> Func(arg); //depending of 'arg' constness, will instance eit=
her 'void
<br>> Func(const float &)' or 'void Func(float &)'
<br>> }
<br>>
<br>>
<br>> And fix the first example as follows:
<br>>
<br>> void Func(float &);
<br>>
<br>>
<br>> void Func(const float &);
<br>>
<br>>
<br>> int main()
<br>> {
<br>> int a;
<br>>
<br>>
<br>> Func((auto_const float &) a); //depending of a constness=
it will be cast
<br>> to either 'const float &' ot 'float &'
<br>>
<br>> }
<br>>
<br>>
<br>> --
<br>>
<br>> ---
<br>> You received this message because you are subscribed to the Google=
Groups
<br>> "ISO C++ Standard - Future Proposals" group.
<br>> To unsubscribe from this group and stop receiving emails from it, =
send an
<br>> email to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-=
mailto=3D"67oeufJCI2EJ" onmousedown=3D"this.href=3D'javascript:';return tru=
e;" onclick=3D"this.href=3D'javascript:';return true;">std-proposal...@<wbr=
>isocpp.org</a>.
<br>> To post to this group, send email to <a href=3D"javascript:" targe=
t=3D"_blank" gdf-obfuscated-mailto=3D"67oeufJCI2EJ" onmousedown=3D"this.hre=
f=3D'javascript:';return true;" onclick=3D"this.href=3D'javascript:';return=
true;">std-pr...@isocpp.org</a>.
<br>> Visit this group at
<br>> <a href=3D"http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/" target=3D"_blank" onmousedown=3D"this.href=3D'http://groups.google.com=
/a/isocpp.org/group/std-proposals/';return true;" onclick=3D"this.href=3D'h=
ttp://groups.google.com/a/isocpp.org/group/std-proposals/';return true;">ht=
tp://groups.google.com/a/<wbr>isocpp.org/group/std-<wbr>proposals/</a>.
<br>>
<br></blockquote></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_3133_505480254.1421263910072--
------=_Part_3132_786346156.1421263910072--
.
Author: Bengt Gustafsson <bengt.gustafsson@beamways.com>
Date: Wed, 14 Jan 2015 16:10:01 -0800 (PST)
Raw View
------=_Part_6175_1082528895.1421280601888
Content-Type: multipart/alternative;
boundary="----=_Part_6176_581680881.1421280601888"
------=_Part_6176_581680881.1421280601888
Content-Type: text/plain; charset=UTF-8
I would say that the prime use case would be to avoid the double
implementation of getters of complex subobjects:
struct Inner {
int a;
int b;
};
class Outer {
public:
auto GetInner() auto_const {
return m_inner;
}
private:
Inner m_inner;
};
In some cases it would be nice to be able to capture the cv-qualification
and put it somewhere else. Then my example above would not have had to use
the auto return type
to get the return type right. I have no good ideas for a syntax for this,
but maybe someone else has? I seem to remember vaguely that this has been
discussed before with nice syntax suggested... I could throw in:
template<const X> X Inner& GetInner() X { return m_inner; }
Here the word const acts similarly as typename in a classic function
template while the trailing X is what gets matched to the call site
qualifier for this and the leading X is a use of the cv-value denoted by X.
Unfortunately this syntax would not work I think as X could be for instance
used by "typedef int X;" in a surrounding scope in which case this probably
already has a meaningful interpretation. I write probably as I am not sure
if const is allowed here, at least it is pretty useless (unless there is a
trailing * or &).
Den tisdag 13 januari 2015 kl. 09:43:46 UTC+1 skrev sasho648:
>
> I think introducing something like auto type qualifier would be useful.
> Here are some examples when.
>
> 1. Casting to another type while preserving constness:
>
> Imagine you have one variable 'a' of type 'int'. You cast and use it
> somewhere in your code as reference to 'float'. After some-time you decide
> that you want this variable to be constant but this could create problems
> as the reference cast wouldn't preserve it!
>
> Examples:
>
> void Func(float &);
>
>
> void Func(const float &);
>
>
> int main()
> {
> int a; // if this becomes constant the below call wouldn't change and
> will still instance 'void Func(float &)' thus creating potentinal problems
>
>
> Func((float &) a);
>
> }
>
>
> 2. Creating type-qualifier general functions.
>
> One example of the above
> <http://stackoverflow.com/questions/27083004/universal-member-function-definition-instanceable-from-both-const-non-cons>
> .
>
> Here is what I purpose (something like):
>
> *auto_const*
>
> *auto_**volatile*
>
> So we could write genereal type-qualifier functions like:
>
> void Func1(auto_const float &arg)
> {
> Func(arg); //depending of 'arg' constness, will instance either 'void
> Func(const float &)' or 'void Func(float &)'
> }
>
>
> And fix the first example as follows:
>
> void Func(float &);
>
>
> void Func(const float &);
>
>
> int main()
> {
> int a;
>
>
> Func((auto_const float &) a); //depending of a constness it will be cast
> to either 'const float &' ot 'float &'
>
> }
>
>
>
--
---
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_6176_581680881.1421280601888
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I would say that the prime use case would be to avoid the =
double implementation of getters of complex subobjects:<div><br></div><div =
class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-w=
rap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"pret=
typrint"><div class=3D"subprettyprint"><font color=3D"#000088"><span style=
=3D"color: #008;" class=3D"styled-by-prettify">struct</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span></font><span style=3D"co=
lor: #606;" class=3D"styled-by-prettify">Inner</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br> </span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"s=
tyled-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=
"><br> </span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> b</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">};</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br><br><br><br></span><span=
style=3D"color: #008;" class=3D"styled-by-prettify">class</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #606;" class=3D"styled-by-prettify">Outer</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </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></span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">public</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
> </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">GetInner</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">()</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> auto_const </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br> &n=
bsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">retur=
n</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> m_inner<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><span =
style=3D"color: #008;" class=3D"styled-by-prettify">private</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br> </span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify">Inner</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> m_inner</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"color: #660;" cla=
ss=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br></span></div></code></div><div><br>In some cases it w=
ould be nice to be able to capture the cv-qualification and put it somewher=
e else. Then my example above would not have had to use the auto return typ=
e</div><div>to get the return type right. I have no good ideas for a syntax=
for this, but maybe someone else has? I seem to remember vaguely that this=
has been discussed before with nice syntax suggested... I could throw in:<=
/div><div><br></div><div>template<const X> X Inner& GetInner() X =
{ return m_inner; }</div><div><br></div><div>Here the word const acts simil=
arly as typename in a classic function template while the trailing X is wha=
t gets matched to the call site qualifier for this and the leading X is a u=
se of the cv-value denoted by X.</div><div><br></div><div>Unfortunately thi=
s syntax would not work I think as X could be for instance used by "typedef=
int X;" in a surrounding scope in which case this probably already has a m=
eaningful interpretation. I write probably as I am not sure if const is all=
owed here, at least it is pretty useless (unless there is a trailing * or &=
amp;).<br><br>Den tisdag 13 januari 2015 kl. 09:43:46 UTC+1 skrev sasho648:=
<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bor=
der-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">I think intro=
ducing something like auto type qualifier would be useful. Here are some ex=
amples when.<br><br>1. Casting to another type while preserving constness:<=
br><div><br></div><div>Imagine you have one variable 'a' of type 'int'. You=
cast and use it somewhere in your code as reference to 'float'. After some=
-time you decide that you want this variable to be constant but this could =
create problems as the reference cast wouldn't preserve it!<br><br>Examples=
:<br><br></div><div style=3D"border:1px solid rgb(187,187,187);word-wrap:br=
eak-word;background-color:rgb(250,250,250)"><code><div><span style=3D"color=
:#008">void</span><span style=3D"color:#000"> </span><span style=3D"color:#=
606">Func</span><span style=3D"color:#660">(</span><span style=3D"color:#00=
8">float</span><span style=3D"color:#000"> </span><span style=3D"color:#660=
">&);</span><span style=3D"color:#000"><br><br><br></span><span style=
=3D"color:#008">void</span><span style=3D"color:#000"> </span><span style=
=3D"color:#606">Func</span><span style=3D"color:#660">(</span><span style=
=3D"color:#008">const</span><span style=3D"color:#000"> </span><span style=
=3D"color:#008">float</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">&);</span><span style=3D"color:#000"><br><br><br></span=
><span style=3D"color:#008">int</span><span style=3D"color:#000"> main</spa=
n><span style=3D"color:#660">()</span><span style=3D"color:#000"><br></span=
><span style=3D"color:#660">{</span><span style=3D"color:#000"><br></span><=
span style=3D"color:rgb(0,0,0)"><span style=3D"color:#000"> </span></s=
pan><span style=3D"color:#008">int</span><span style=3D"color:#000"> a</spa=
n><span style=3D"color:#660">;</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#800">// if this becomes constant the below call wouldn't=
change and will still instance 'void Func(float &)' thus creating pote=
ntinal problems</span><span style=3D"color:#000"><br><br><br> </span><=
span style=3D"color:#606">Func</span><span style=3D"color:#660">((</span><s=
pan style=3D"color:#008">float</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">&)</span><span style=3D"color:#000"> a</span><s=
pan style=3D"color:#660">);</span><span style=3D"color:#000"><br><br></span=
><span style=3D"color:#660">}</span><span style=3D"color:#000"><br></span><=
/div></code></div><div><br><br></div><div>2. Creating type-qualifier genera=
l functions.<br></div><div><br></div><div>One example of the <a href=3D"htt=
p://stackoverflow.com/questions/27083004/universal-member-function-definiti=
on-instanceable-from-both-const-non-cons" target=3D"_blank" onmousedown=3D"=
this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2=
Fquestions%2F27083004%2Funiversal-member-function-definition-instanceable-f=
rom-both-const-non-cons\46sa\75D\46sntz\0751\46usg\75AFQjCNEr2rplfF4eBtvi0i=
tgNFA_eZr1BA';return true;" onclick=3D"this.href=3D'http://www.google.com/u=
rl?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F27083004%2Funiversal-me=
mber-function-definition-instanceable-from-both-const-non-cons\46sa\75D\46s=
ntz\0751\46usg\75AFQjCNEr2rplfF4eBtvi0itgNFA_eZr1BA';return true;">above</a=
>.</div><div><br></div><div>Here is what I purpose (something like):<br><br=
><b>auto_const</b></div><div><b><br></b></div><div><b>auto_</b><span style=
=3D"color:rgb(0,0,0);font-family:DejaVuSans,'DejaVu Sans',arial,sans-serif;=
font-size:12.8000001907349px;line-height:15.3600006103516px"><b>volatile</b=
></span></div><div><span style=3D"color:rgb(0,0,0);font-family:DejaVuSans,'=
DejaVu Sans',arial,sans-serif;font-size:12.8000001907349px;line-height:15.3=
600006103516px"><b><br></b></span></div><div>So we could write genereal typ=
e-qualifier functions like:</div><div><br></div><div style=3D"border:1px so=
lid rgb(187,187,187);word-wrap:break-word;background-color:rgb(250,250,250)=
"><code><div><span style=3D"color:#008">void</span><span style=3D"color:#00=
0"> </span><span style=3D"color:#606">Func1</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">auto_const </span><span style=3D=
"color:#008">float</span><span style=3D"color:#000"> </span><span style=3D"=
color:#660">&</span><span style=3D"color:#000">arg</span><span style=3D=
"color:#660">)</span><span style=3D"color:#000"><br></span><span style=3D"c=
olor:#660">{</span><span style=3D"color:#000"><br> </span><span style=
=3D"color:#606">Func</span><span style=3D"color:#660">(</span><span style=
=3D"color:#000">arg</span><span style=3D"color:#660">);</span><span style=
=3D"color:#000"> </span><span style=3D"color:#800">//depending of 'arg' con=
stness, will instance either 'void Func(const float &)' or 'void Func(f=
loat &)'</span><span style=3D"color:#000"><br></span><span style=3D"col=
or:#660">}</span></div></code></div><div><br></div><div><br></div><div>And =
fix the first example as follows:<br><br><span style=3D"font-family:monospa=
ce;color:rgb(0,0,136);background-color:rgb(250,250,250)"></span></div><div =
style=3D"border:1px solid rgb(187,187,187);word-wrap:break-word;background-=
color:rgb(250,250,250)"><code><div><span style=3D"color:#008">void</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#606">Func</span><spa=
n style=3D"color:#660">(</span><span style=3D"color:#008">float</span><span=
style=3D"color:#000"> </span><span style=3D"color:#660">&);</span><spa=
n style=3D"color:#000"><br><br><br></span><span style=3D"color:#008">void</=
span><span style=3D"color:#000"> </span><span style=3D"color:#606">Func</sp=
an><span style=3D"color:#660">(</span><span style=3D"color:#008">const</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#008">float</span=
><span style=3D"color:#000"> </span><span style=3D"color:#660">&);</spa=
n><span style=3D"color:#000"><br><br><br></span><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:#000"><br> </span><span style=3D"color:#0=
08">int</span><span style=3D"color:#000"> a</span><span style=3D"color:#660=
">;</span><span style=3D"color:#000"><br><br><br> </span><span style=
=3D"color:#606">Func</span><span style=3D"color:#660">((</span><span style=
=3D"color:#000">auto_const </span><span style=3D"color:#008">float</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#660">&)</span><s=
pan style=3D"color:#000"> a</span><span style=3D"color:#660">);</span><span=
style=3D"color:#000"> </span><span style=3D"color:#800">//depending of a c=
onstness it will be cast to either 'const float &' ot 'float &'</sp=
an><span style=3D"color:#000"><br><br></span><span style=3D"color:#660">}</=
span></div></code></div><div><span style=3D"font-family:monospace;color:rgb=
(102,102,0);background-color:rgb(250,250,250)"><br></span><br></div></div><=
/blockquote></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_6176_581680881.1421280601888--
------=_Part_6175_1082528895.1421280601888--
.