Is there any difference between the term "visibility" and
"scope" of a variable.
Readhttp://
www.eskimo.com/~scs/cclass/notes/sx4b.html article.
According to my understanding it says the two terms are
equivalent. Am I right?
Well, the most obvious distinction here is that in C++, scope is
defined by the C++ standard (section 3.3). Visibility isn't, so
presumably, it can be used to mean whatever you want it to mean.
Generally, however, I would understand visibility to mean that
the name can be found by some form of name lookup (section 3.4
of the standard). For example:
namespace Toto {
int i ;
}
void
f()
{
// i not in "scope" here, but since I can write
// Toto::i, it's visible?
}
One could argue, however, that in this case, you've specified a
scope, and the variable is visible because of the specified
scope.
Two more interesting cases concern ADL and using directives. In
the first, the compiler automatically makes certain scopes
"visible", e.g.:
namespace Toto {
class Titi {} ;
void f( Titi const& ) ;
}
void
g()
{
Toto::Titi tata ;
f( tata ) ; // finds Toto::f
}
In the second, the standard seems to make sort of a distinction
itself: "A using-directive specifies that the names in the
nominated namespace can be used in the scope in which the
usingdirective appears after the using-directive. During
unqualified name lookup (3.4.1), the names appear as if they
were declared in the nearest enclosing namespace which contains
both the using-directive and the nominated namespace." (The use
of the words "as if" suggests that the names aren't in this
scope, but will be considered "as if" they were for the purposes
of visibility.)
Generally speaking, however, I'd say that the main difference is
one of point of view: scope is considered from the point of view
of the declaration (the declaration is valid where), visibility
from the point of view of use (is the declaration visible at
this point). In a lot of cases, the visibility is determined by
the scope of the declaration being considered.