We all knew every programming language has its own approaches to introduce new identifies into current context. In C++, we called them declarations.
On the opposite side, do we have explicit undeclare facility in C++?
For example:
1 2 3 4 5 6 7 8 9 10
ThreadContext * pctxt = (ThreadContext *) arg;
ThreadContext context = *pctxt;
pctxt = NULL; // need explicit undeclare language element here
// Candidate syntax may be
// using delete pctxt;
// If 'pctxt' is explicitly undeclared,
// a) subsequent use for 'pctxt' may generate compiler error/warning
// b) or, we can reuse the 'pctxt' identify. // int pctxt = 0;
Note, explicit undeclare is different from local block trick
1 2 3 4 5 6
ThreadContext context; // default-constructor is called here
{
ThreadContext * pctxt = (ThreadContext *) arg;
context = *pctxt; // copy-constructor is called here.
pctxt = NULL; // pctxt will be inaccessible out the block
}
Explicit undeclare syntax may bring many benefits into our life.
1. Avoid inattention use for unwanted association/dependency/...
2. Provide hints for code optimization
3. ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void foo(int arg)
{
// using delete default; //avoid to use any outside identifies, except function arguments. ('functional'?)
}
void Foo::bar(int arg)
{
// using default class; //only class-wide (include ancestors') identifies can be used in the block.
// using default this class; // only current class identifies(static-/member-wide) can be used.
}
// using default this namespace; // only identifies under surrounding namespace can be used.
// using default static; // only translation-union wide (somewhat file-wide) identifies can be used.
// using delete ... // explicit undeclare
// using default ... // explicit restrict up-most identify introduction.
Please recall the new coming C++11 '= default', '= delete' language element. I think our new explicit undeclare syntax maybe somewhat like them.
What I expected is a general identify undeclare syntax.
For example:
1 2 3 4 5
int foo = 0;
int temp = foo;
usingdelete temp; // imagined sytax ... like #undef in preprocessor
int bar = temp; // compiler error -- 'temp' is undefined.
Where can I submit/disucss/request this new language feature for C++?
int foo = 0;
{
int temp = foo;
} // using delete temp; // imagined sytax ... like #undef in preprocessor
int bar = temp; // compiler error -- 'temp' is undefined.
Another option may be to assert. If you insist, you could wrap the assertion in a macro so it can be compiled out in release builds. I still don't see why you'd want to leave an unusable pointer in scope, though...