0
votes

I'm reading Programming Pragmatics and it mentions that:

C++ is representative of an increasingly common option, in which names are automatically exported, but are available on the outside only when qualified with the module name—unless they are explicitly “imported” by another scope (e.g., with the C++ using directive), at which point they are available unqualified.

I thought that all names in C++ are exported only when another module imports it.

If names of module A are automatically exported, why do we have to use #include in other modules? I thought the #include __ feature was similar to import __ in Python.

What is the syntax for qualifying the module name outside the module so that we can use its data without an explicit using directive?

Please enlighten me. Thank you very much in advance.

1
It sounds like it is talking about namespaces. Like #include <string> std::string versus #include <string> using namespace std; stringRetired Ninja
#include has nothing to do with importing things across modules.Remy Lebeau
I think you should find a different book. While that sentence isn't actually wrong, it presents a muddled notion of how names work in C++ that uses terms and concepts that aren't part of the C++ language. Very confusing.Pete Becker

1 Answers

1
votes

That sentence seems to be talking about qualified versus unqualified names, but it uses terms and concepts that simply don't belong to C++.

#include <iostream>

int main() {
    std::cout << "Hello, world\n";
    return 0;
}

#include <iostream>
using namespace std; // we never do this

int main() {
    cout << "Hello, world\n";
    return 0;
}

#include <iostream>
using std::cout; // some people like this

int main() {
    cout << "Hello, world\n";
    return 0;
}

The first is the right way to do it.