Anonymous or unnamed variables

83 views
Skip to first unread message

Vicente J. Botet Escriba

unread,
Nov 13, 2012, 4:46:58 PM11/13/12
to std-pr...@isocpp.org
Hi,

Quite often we need to name a guard variable which is not used other
than in the declaration to do some action on the destruction (raii), e.g.

{
std::lock_guard<std::mutex> guard(mtx);
// ...
}

Giving a name to these guard variables doesn't adds value to the
expression, and some of us use to name them '_'

{
std::lock_guard<std::mutex> _(mtx);
// ...
}

Of course we can not include two of them on the same scope without using
a more specific name as the name conflict.

{
std::lock_guard<std::mutex> g1(mtx1);
std::lock_guard<std::mutex> g2(mtx2);
// ...
}

Do you think it could be worth adding some kind of anonymous variable
that avoid having to name these kind of variables? E.g.we could use the
token '...'

{
std::lock_guard<std::mutex> ...(mtx1);
std::lock_guard<std::mutex> ...(mtx2);
// ...
}

One of the features of these unnamed variables could be that we can not
do anything with, so maybe the compiler could take advantage in order to
optimize the code.

I recognize that the added value is minor, but I'm sure that you have
already desired to not name these variables.

Best,
Vicente

Nevin Liber

unread,
Nov 13, 2012, 5:25:41 PM11/13/12
to std-pr...@isocpp.org
On 13 November 2012 15:46, Vicente J. Botet Escriba <vicent...@wanadoo.fr> wrote:

One of the features of these unnamed variables could be that we can not do anything with, so maybe the compiler could take advantage in order to optimize the code.

I can't imagine how.  Do you have a scenario in mind?
 
I recognize that the added value is minor, but I'm sure that you have already desired to not name these variables.

Given the cost to adding language features, I just don't see a compelling case.
--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com(847) 691-1404

Vicente J. Botet Escriba

unread,
Nov 14, 2012, 1:11:53 AM11/14/12
to std-pr...@isocpp.org
Le 13/11/12 23:25, Nevin Liber a écrit :
On 13 November 2012 15:46, Vicente J. Botet Escriba <vicent...@wanadoo.fr> wrote:

One of the features of these unnamed variables could be that we can not do anything with, so maybe the compiler could take advantage in order to optimize the code.

I can't imagine how.  Do you have a scenario in mind?
No, As I said I'm not a compiler writer :(

 
I recognize that the added value is minor, but I'm sure that you have already desired to not name these variables.

Given the cost to adding language features, I just don't see a compelling case.

I just wanted to share a minor need that 'don't name what you don't need' ;-).
I understand that we can concentrate in more important features.

Vicente

Reply all
Reply to author
Forward
0 new messages