How to explicit undeclare variable in C++

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.
Last edited on
1
2
3
ThreadContext * pctxt = (ThreadContext *) arg;
ThreadContext context = *pctxt;
pctxt = NULL;  // need explicit undeclare language element here 


C++ 0x introduces nullptr

ThreadContext context = *pctxt;
pctxt = nullptr;

nullptr can't be compared nor aritmetic operations can be done with nullptr.
What I expected is a general identify undeclare syntax.

For example:

1
2
3
4
5
int foo = 0;
int temp = foo;
using delete 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++?
Last edited on
This can be done with scope:
1
2
3
4
5
6
int foo = 0;
{
int temp = foo;
} // using delete temp; // imagined sytax ... like #undef in preprocessor

int bar = temp; // compiler error -- 'temp' is undefined.  
I think the best solution for that kind are namespaces,
1
2
3
4
5
6
7
8
9
10
namespace test {
	int a = 5;
}

int main(int argc, char* argv []) {
        cout << a;       //a is undefined
	using namespace test;
	cout << a;
	return 0;
}

Where can I submit/disucss/request this new language feature for C++?

I dont know,
but if you search the web for such places you will find it for shure.
Last edited on
I was thinkiing that not everything is possible in C++....

1
2
3
4
5
int a = 5;
cout << a;    // OK 

#pragma deprecated ( a )
cout << a;     //error a does not exist any more 


...learning new things makes things possible :D
Oh! That's it.

I've checked the document, this is Visual C++'s preprocessor syntax.

 
#pragma deprecated (a)  


Wher in GCC, we can use

 
#pragma GCC posion a 


Although this is not a portable solution. But, I think we can _Pragma to handle it. I will try them in my project.

The power of MACRO !
Last edited on
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...
Topic archived. No new replies allowed.