Topic: forget(varname): prevent further use of a variable


Author: "U.Mutlu" <for-gmane@mutluit.com>
Date: Sun, 18 Jan 2015 23:22:25 +0100
Raw View
Hello,
I want to propose the following feature be added into the language or the
preprocessor:

It would be a useful feature if one could prevent
the further use of a variable by writing "forget(varname)"
or "hide(varname)" or "undefine(varname)".

This would especially be useful in refactoring/maintaining old code, and also
for debugging.

Of course the implicit calling of its destructor, if any,
at the end of the scope, should of course remain.

Example:
  //...
  int x, y = 3, z = 4;
  //...
  x = y * z
  //...
  forget(x);  // the proposed new language feature
  x *= z;     // shall bring compile error
  //...

I know one can use inner scopes, but this makes the code look cumbersome.
The above proposed solution would be a very useful feature for code
maintainers of code written by someone else.

In the meantime: if someone knows a workaround solution (possibly using a
preprocessor macro) let me know please.

cu
Uenal

--

---
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: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Mon, 19 Jan 2015 00:27:08 +0200
Raw View
On 19 January 2015 at 00:22, U.Mutlu <for-gmane@mutluit.com> wrote:
> Hello,
> I want to propose the following feature be added into the language or the
> preprocessor:
>
> It would be a useful feature if one could prevent
> the further use of a variable by writing "forget(varname)"
> or "hide(varname)" or "undefine(varname)".


Seems like you're repeating
https://groups.google.com/a/isocpp.org/d/msg/std-proposals/LhKCvaxAbL4/wsxYjhB10-0J

--

---
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: "U.Mutlu" <for-gmane@mutluit.com>
Date: Sun, 18 Jan 2015 23:38:58 +0100
Raw View
Ville Voutilainen wrote, On 01/18/2015 11:27 PM:
> On 19 January 2015 at 00:22, U.Mutlu <for-gmane@mutluit.com> wrote:
>> Hello,
>> I want to propose the following feature be added into the language or the
>> preprocessor:
>>
>> It would be a useful feature if one could prevent
>> the further use of a variable by writing "forget(varname)"
>> or "hide(varname)" or "undefine(varname)".
>
>
> Seems like you're repeating
> https://groups.google.com/a/isocpp.org/d/msg/std-proposals/LhKCvaxAbL4/wsxYjhB10-0J

Yes it's essentially the same mechanism.
I wasn't aware about the above posting by Matthew Fioravante dated 8/20/14.
There it's called "kill varname", yeah that naming scheme would be fine too.

cu
Uenal


--

---
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: Douglas Boffey <douglas.boffey@gmail.com>
Date: Sun, 18 Jan 2015 23:34:43 +0000
Raw View
int x;
if (/* whatever */)
  forget(x);
cout << x; // legal?


On 1/18/15, U.Mutlu <for-gmane@mutluit.com> wrote:
> Ville Voutilainen wrote, On 01/18/2015 11:27 PM:
>> On 19 January 2015 at 00:22, U.Mutlu <for-gmane@mutluit.com> wrote:
>>> Hello,
>>> I want to propose the following feature be added into the language or
>>> the
>>> preprocessor:
>>>
>>> It would be a useful feature if one could prevent
>>> the further use of a variable by writing "forget(varname)"
>>> or "hide(varname)" or "undefine(varname)".
>>
>>
>> Seems like you're repeating
>> https://groups.google.com/a/isocpp.org/d/msg/std-proposals/LhKCvaxAbL4/wsxYjhB10-0J
>
> Yes it's essentially the same mechanism.
> I wasn't aware about the above posting by Matthew Fioravante dated 8/20/14.
> There it's called "kill varname", yeah that naming scheme would be fine
> too.
>
> cu
> Uenal
>
>
> --
>
> ---
> 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: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Sun, 18 Jan 2015 23:36:09 +0000
Raw View
2015-01-18 23:34 GMT+00:00 Douglas Boffey <douglas.boffey@gmail.com>:
> int x;
> if (/* whatever */)
>   forget(x);
> cout << x; // legal?
>
>

Yes, I propose this is legal and throw std::illegal_use_error();

--

---
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, 19 Jan 2015 07:40:17 +0800
Raw View
--Apple-Mail=_A308583C-FCD1-43F1-93F1-1335D3284ECF
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8


> On 2015=E2=80=9301=E2=80=9319, at 7:36 AM, Johannes Schaub <schaub.johann=
es@googlemail.com> wrote:
>=20
> 2015-01-18 23:34 GMT+00:00 Douglas Boffey <douglas.boffey@gmail.com>:
>> int x;
>> if (/* whatever */)
>>  forget(x);
>> cout << x; // legal?
>>=20
>>=20
>=20
> Yes, I propose this is legal and throw std::illegal_use_error();

=E2=80=A6 unless it=E2=80=99s inside a preemptive_catch {} block

--=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=_A308583C-FCD1-43F1-93F1-1335D3284ECF
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dutf-8"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: s=
pace; -webkit-line-break: after-white-space;" class=3D""><br class=3D""><di=
v><blockquote type=3D"cite" class=3D""><div class=3D"">On 2015=E2=80=9301=
=E2=80=9319, at 7:36 AM, Johannes Schaub &lt;<a href=3D"mailto:schaub.johan=
nes@googlemail.com" class=3D"">schaub.johannes@googlemail.com</a>&gt; wrote=
:</div><br class=3D"Apple-interchange-newline"><div class=3D"">2015-01-18 2=
3:34 GMT+00:00 Douglas Boffey &lt;<a href=3D"mailto:douglas.boffey@gmail.co=
m" class=3D"">douglas.boffey@gmail.com</a>&gt;:<br class=3D""><blockquote t=
ype=3D"cite" class=3D"">int x;<br class=3D"">if (/* whatever */)<br class=
=3D""> &nbsp;forget(x);<br class=3D"">cout &lt;&lt; x; // legal?<br class=
=3D""><br class=3D""><br class=3D""></blockquote><br class=3D"">Yes, I prop=
ose this is legal and throw std::illegal_use_error();<br class=3D""></div><=
/blockquote><div><br class=3D""></div><div>=E2=80=A6 unless it=E2=80=99s in=
side a <font face=3D"Courier" class=3D"">preemptive_catch {}</font> block</=
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&quot; 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=_A308583C-FCD1-43F1-93F1-1335D3284ECF--

.


Author: Douglas Boffey <douglas.boffey@gmail.com>
Date: Sun, 18 Jan 2015 23:51:11 +0000
Raw View
Regarding the preprocessor, something like #poison would be better (IMO).

On 1/18/15, David Krauss <potswa@gmail.com> wrote:
>
>> On 2015=E2=80=9301=E2=80=9319, at 7:36 AM, Johannes Schaub
>> <schaub.johannes@googlemail.com> wrote:
>>
>> 2015-01-18 23:34 GMT+00:00 Douglas Boffey <douglas.boffey@gmail.com>:
>>> int x;
>>> if (/* whatever */)
>>>  forget(x);
>>> cout << x; // legal?
>>>
>>>
>>
>> Yes, I propose this is legal and throw std::illegal_use_error();
>
> =E2=80=A6 unless it=E2=80=99s inside a preemptive_catch {} block
>
> --
>
> ---
> 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/.
>

--=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/.

.


Author: "U.Mutlu" <for-gmane@mutluit.com>
Date: Mon, 19 Jan 2015 00:57:29 +0100
Raw View
U.Mutlu wrote, On 01/18/2015 11:38 PM:
> Ville Voutilainen wrote, On 01/18/2015 11:27 PM:
>> On 19 January 2015 at 00:22, U.Mutlu <for-gmane@mutluit.com> wrote:
>>> Hello,
>>> I want to propose the following feature be added into the language or t=
he
>>> preprocessor:
>>>
>>> It would be a useful feature if one could prevent
>>> the further use of a variable by writing "forget(varname)"
>>> or "hide(varname)" or "undefine(varname)".
>>
>>
>> Seems like you're repeating
>> https://groups.google.com/a/isocpp.org/d/msg/std-proposals/LhKCvaxAbL4/w=
sxYjhB10-0J
>>
>
> Yes it's essentially the same mechanism.
> I wasn't aware about the above posting by Matthew Fioravante dated 8/20/1=
4.
> There it's called "kill varname", yeah that naming scheme would be fine t=
oo.

I've found the following workaround (early beta workaround version):


// compiler will print this error string in its error output
#define forget "variable was marked as 'forget'"


void func()
{
   int x =3D 123;
   //...
   #define x forget
   //...
   x =3D 456;
   //...
}

int main()
{
   func();
   return 0;
}


$ g++ -Wall forget_demo.cpp
forget_demo.cpp: In function =E2=80=98void func()=E2=80=99:
forget_demo.cpp:12:5: error: assignment of read-only location =E2=80=98"var=
iable was=20
marked as \'forget\'"=E2=80=99
    x =3D 456;
      ^

This works for all non-string variables, but I guess it will be problematic=
=20
with string variables (not tested yet)...

cu
Uenal



--=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/.

.


Author: germinolegrand <germinolegrand@gmail.com>
Date: Mon, 19 Jan 2015 01:01:32 +0100
Raw View
Le 19/01/2015 00:57, U.Mutlu a =C3=A9crit :
>
> I've found the following workaround (early beta workaround version):
>
>
> // compiler will print this error string in its error output
> #define forget "variable was marked as 'forget'"
>
>
> void func()
> {
>   int x =3D 123;
>   //...
>   #define x forget
>   //...
>   x =3D 456;
>   //...
> }
>
> int main()
> {
>   func();
>   return 0;
> }
>
>
> $ g++ -Wall forget_demo.cpp
> forget_demo.cpp: In function =E2=80=98void func()=E2=80=99:
> forget_demo.cpp:12:5: error: assignment of read-only location=20
> =E2=80=98"variable was marked as \'forget\'"=E2=80=99
>    x =3D 456;
>      ^
>
> This works for all non-string variables, but I guess it will be=20
> problematic with string variables (not tested yet)...
>
> cu
> Uenal
>
If you do that, you will need a #undef somewhere, because any use of the=20
name 'x' after forget, even out of scope, will cause an error.

--=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/.

.


Author: "U.Mutlu" <for-gmane@mutluit.com>
Date: Mon, 19 Jan 2015 01:16:04 +0100
Raw View
germinolegrand wrote, On 01/19/2015 01:01 AM:
> Le 19/01/2015 00:57, U.Mutlu a =C3=A9crit :
>>
>> This works for all non-string variables, but I guess it will be problema=
tic
>> with string variables (not tested yet)...
>>
>> cu
>> Uenal
>>
> If you do that, you will need a #undef somewhere, because any use of the =
name
> 'x' after forget, even out of scope, will cause an error.

Yes, very true. Here's an update:


// compiler will print this error string in its error output
#define forget "variable was marked as 'forget'"


bool f1()
{
   int x =3D 123;
   //...
   const int y =3D x * 2;
   #define x forget
   //...
   x =3D 456;
   //...

   return !y;

   // necessary:
   #undef x
}

bool f2()
{
   int x =3D 234;
   //...

   return !x;
}

int main()
{
   f1();
   f2();
   return 0;
}



--=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/.

.


Author: "U.Mutlu" <for-gmane@mutluit.com>
Date: Mon, 19 Jan 2015 02:22:48 +0100
Raw View
/*
A workaround solution for "forget(varname)"

This version works with variables of any type.

Usage:
   One has to use "#define varname forget"
   And later (at the end of the func) use "#undef varname"
*/

// --- my.hpp ---
#ifndef my_hpp
#define my_hpp
#include <string>
class forgetit
{ public:
     // this is a necessary dummy class
     // no other class should have the same signature
     forgetit(int, char*, int = 2, long = 3, char = '4', double = 5.0, float =
6.0) {}
};
// compiler will print this error string in its error output:
#define forget forgetit(0, "variable was marked as 'forget'")
#endif


// --- my.cpp ---
#include "my.hpp"

bool f1()
{
    int x = 123;
    //...
    #define x forget
    //...
// x = 456;   // enabling this causes compile error, which is good
    //...
    #undef x
    return !x;
}

bool f2()
{ // c-string
    const char* x = "bla";
    //...
    #define x forget
// x = "foo";   // enabling this causes compile error, which is good
    //...
    #undef x
    return x[0] != 0;
}

bool f3()
{ // c++-string class
    std::string x = "bla";
    //...
    #define x forget
// x = "foo";   // enabling this causes compile error, which is good
    //...
    #undef x
    return !x.empty();
}

class C
{
   public:
     C() {}
};
bool f4()
{ // any class
    C x, y;
    //...
    #define x forget
// x = y;   // enabling this causes compile error, which is good
    #undef x
    return true;
}

int main()
{
    f1();
    f2();
    f3();
    f4();
    return 0;
}


--

---
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: Brent Friedman <fourthgeek@gmail.com>
Date: Mon, 19 Jan 2015 03:17:32 -0600
Raw View
--001a113d6a52d95a2b050cfdca41
Content-Type: text/plain; charset=UTF-8

Why not just use optional?

On Sun, Jan 18, 2015 at 7:22 PM, U.Mutlu <for-gmane@mutluit.com> wrote:

> /*
> A workaround solution for "forget(varname)"
>
> This version works with variables of any type.
>
> Usage:
>   One has to use "#define varname forget"
>   And later (at the end of the func) use "#undef varname"
> */
>
> // --- my.hpp ---
> #ifndef my_hpp
> #define my_hpp
> #include <string>
> class forgetit
> { public:
>     // this is a necessary dummy class
>     // no other class should have the same signature
>     forgetit(int, char*, int = 2, long = 3, char = '4', double = 5.0,
> float = 6.0) {}
> };
> // compiler will print this error string in its error output:
> #define forget forgetit(0, "variable was marked as 'forget'")
> #endif
>
>
> // --- my.cpp ---
> #include "my.hpp"
>
> bool f1()
> {
>    int x = 123;
>    //...
>    #define x forget
>    //...
> // x = 456;   // enabling this causes compile error, which is good
>    //...
>    #undef x
>    return !x;
> }
>
> bool f2()
> { // c-string
>    const char* x = "bla";
>    //...
>    #define x forget
> // x = "foo";   // enabling this causes compile error, which is good
>    //...
>    #undef x
>    return x[0] != 0;
> }
>
> bool f3()
> { // c++-string class
>    std::string x = "bla";
>    //...
>    #define x forget
> // x = "foo";   // enabling this causes compile error, which is good
>    //...
>    #undef x
>    return !x.empty();
> }
>
> class C
> {
>   public:
>     C() {}
> };
> bool f4()
> { // any class
>    C x, y;
>    //...
>    #define x forget
> // x = y;   // enabling this causes compile error, which is good
>    #undef x
>    return true;
> }
>
> int main()
> {
>    f1();
>    f2();
>    f3();
>    f4();
>
>    return 0;
> }
>
>
> --
>
> --- 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/.

--001a113d6a52d95a2b050cfdca41
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Why not just use optional?</div><div class=3D"gmail_extra"=
><br><div class=3D"gmail_quote">On Sun, Jan 18, 2015 at 7:22 PM, U.Mutlu <s=
pan dir=3D"ltr">&lt;<a href=3D"mailto:for-gmane@mutluit.com" target=3D"_bla=
nk">for-gmane@mutluit.com</a>&gt;</span> wrote:<br><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex">/*<br>
A workaround solution for &quot;forget(varname)&quot;<br>
<br>
This version works with variables of any type.<br>
<br>
Usage:<br>
=C2=A0 One has to use &quot;#define varname forget&quot;<br>
=C2=A0 And later (at the end of the func) use &quot;#undef varname&quot;<br=
>
*/<br>
<br>
// --- my.hpp ---<br>
#ifndef my_hpp<br>
#define my_hpp<br>
#include &lt;string&gt;<br>
class forgetit<br>
{ public:<br>
=C2=A0 =C2=A0 // this is a necessary dummy class<br>
=C2=A0 =C2=A0 // no other class should have the same signature<br>
=C2=A0 =C2=A0 forgetit(int, char*, int =3D 2, long =3D 3, char =3D &#39;4&#=
39;, double =3D 5.0, float =3D 6.0) {}<span class=3D""><br>
};<br>
// compiler will print this error string in its error output:<br></span>
#define forget forgetit(0, &quot;variable was marked as &#39;forget&#39;&qu=
ot;)<br>
#endif<br>
<br>
<br>
// --- my.cpp ---<br>
#include &quot;my.hpp&quot;<span class=3D""><br>
<br>
bool f1()<br>
{<br>
=C2=A0 =C2=A0int x =3D 123;<br>
=C2=A0 =C2=A0//...<br></span>
=C2=A0 =C2=A0#define x forget<br>
=C2=A0 =C2=A0//...<br>
// x =3D 456;=C2=A0 =C2=A0// enabling this causes compile error, which is g=
ood<br>
=C2=A0 =C2=A0//...<br>
=C2=A0 =C2=A0#undef x<br>
=C2=A0 =C2=A0return !x;<br>
}<br>
<br>
bool f2()<br>
{ // c-string<br>
=C2=A0 =C2=A0const char* x =3D &quot;bla&quot;;<br>
=C2=A0 =C2=A0//...<br>
=C2=A0 =C2=A0#define x forget<br>
// x =3D &quot;foo&quot;;=C2=A0 =C2=A0// enabling this causes compile error=
, which is good<br>
=C2=A0 =C2=A0//...<br>
=C2=A0 =C2=A0#undef x<br>
=C2=A0 =C2=A0return x[0] !=3D 0;<br>
}<br>
<br>
bool f3()<br>
{ // c++-string class<br>
=C2=A0 =C2=A0std::string x =3D &quot;bla&quot;;<br>
=C2=A0 =C2=A0//...<br>
=C2=A0 =C2=A0#define x forget<br>
// x =3D &quot;foo&quot;;=C2=A0 =C2=A0// enabling this causes compile error=
, which is good<br>
=C2=A0 =C2=A0//...<br>
=C2=A0 =C2=A0#undef x<br>
=C2=A0 =C2=A0return !x.empty();<br>
}<br>
<br>
class C<br>
{<br>
=C2=A0 public:<br>
=C2=A0 =C2=A0 C() {}<br>
};<br>
bool f4()<br>
{ // any class<br>
=C2=A0 =C2=A0C x, y;<br>
=C2=A0 =C2=A0//...<br>
=C2=A0 =C2=A0#define x forget<br>
// x =3D y;=C2=A0 =C2=A0// enabling this causes compile error, which is goo=
d<br>
=C2=A0 =C2=A0#undef x<br>
=C2=A0 =C2=A0return true;<br>
}<br>
<br>
int main()<br>
{<br>
=C2=A0 =C2=A0f1();<br>
=C2=A0 =C2=A0f2();<br>
=C2=A0 =C2=A0f3();<br>
=C2=A0 =C2=A0f4();<div class=3D"HOEnZb"><div class=3D"h5"><br>
=C2=A0 =C2=A0return 0;<br>
}<br>
<br>
<br>
-- <br>
<br>
--- You received this message because you are subscribed to the Google Grou=
ps &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org" target=3D=
"_blank">std-proposals+unsubscribe@<u></u>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">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/<u></u>isocpp.=
org/group/std-<u></u>proposals/</a>.<br>
</div></div></blockquote></div><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&quot; 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 />

--001a113d6a52d95a2b050cfdca41--

.


Author: "U.Mutlu" <for-gmane@mutluit.com>
Date: Mon, 19 Jan 2015 10:33:18 +0100
Raw View
Brent Friedman wrote, On 01/19/2015 10:17 AM:
> Why not just use optional?

Do you mean this:
http://en.cppreference.com/w/cpp/experimental/optional

cite:
After reviewing national body comments to N3690, this library component was
voted out from C++14 working paper into a separate Technical Specification. It
is not a part of the draft C++14 as of n3797.

I'll play with it if I find it in my compiler.


> On Sun, Jan 18, 2015 at 7:22 PM, U.Mutlu <for-gmane@mutluit.com> wrote:
>
>> /*
>> A workaround solution for "forget(varname)"
>>
>> This version works with variables of any type.
>>
>> Usage:
>>    One has to use "#define varname forget"
>>    And later (at the end of the func) use "#undef varname"
>> */
>>
>> // --- my.hpp ---
>> #ifndef my_hpp
>> #define my_hpp
>> #include <string>
>> class forgetit
>> { public:
>>      // this is a necessary dummy class
>>      // no other class should have the same signature
>>      forgetit(int, char*, int = 2, long = 3, char = '4', double = 5.0,
>> float = 6.0) {}
>> };
>> // compiler will print this error string in its error output:
>> #define forget forgetit(0, "variable was marked as 'forget'")
>> #endif
>>
>>
>> // --- my.cpp ---
>> #include "my.hpp"
>>
>> bool f1()
>> {
>>     int x = 123;
>>     //...
>>     #define x forget
>>     //...
>> // x = 456;   // enabling this causes compile error, which is good
>>     //...
>>     #undef x
>>     return !x;
>> }
>>
>> bool f2()
>> { // c-string
>>     const char* x = "bla";
>>     //...
>>     #define x forget
>> // x = "foo";   // enabling this causes compile error, which is good
>>     //...
>>     #undef x
>>     return x[0] != 0;
>> }
>>
>> bool f3()
>> { // c++-string class
>>     std::string x = "bla";
>>     //...
>>     #define x forget
>> // x = "foo";   // enabling this causes compile error, which is good
>>     //...
>>     #undef x
>>     return !x.empty();
>> }
>>
>> class C
>> {
>>    public:
>>      C() {}
>> };
>> bool f4()
>> { // any class
>>     C x, y;
>>     //...
>>     #define x forget
>> // x = y;   // enabling this causes compile error, which is good
>>     #undef x
>>     return true;
>> }
>>
>> int main()
>> {
>>     f1();
>>     f2();
>>     f3();
>>     f4();
>>
>>     return 0;
>> }
>>
>>
>> --
>>
>> --- 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: Jim Porter <jvp4846@g.rit.edu>
Date: Mon, 19 Jan 2015 13:15:35 -0600
Raw View
On 1/19/2015 3:33 AM, U.Mutlu wrote:
> Brent Friedman wrote, On 01/19/2015 10:17 AM:
>> Why not just use optional?
>
[snip]
>
> I'll play with it if I find it in my compiler.

Luckily for you, it's already in Boost[1], libc++[2], and libstdc++[3],
and will probably be standardized in a TS[4] before C++17.

- Jim

[1] http://www.boost.org/doc/libs/1_57_0/libs/optional/doc/html/index.html
[2]
http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/experimental/optional?revision=223775&view=markup
[3]
https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/include/experimental/optional?revision=219188&view=markup
[4]
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4335.html#optional

--

---
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/.

.