A using declaration brings one selected name from a namespace into the current scope.

using declaration A using declaration names exactly what should be brought into the current scope.

Using Declarations

name
using_declarations.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>

namespace greetings {
std::string hello(const std::string& name) {
    return "Hello, " + name;
}
}

int main() {
    using greetings::hello;

    std::string name = "Ada";

    std::string message = hello(name);

    std::cout << "name=" << name << std::endl;
    std::cout << "message=" << message << std::endl;
    return 0;
}
#include <iostream>
#include <string>

namespace greetings {
std::string hello(const std::string& name) {
    return "Hello, " + name;
}
}

int main() {
    using greetings::hello;

    std::string name = "Bjarne";

    std::string message = hello(name);

    std::cout << "name=" << name << std::endl;
    std::cout << "message=" << message << std::endl;
    return 0;
}
#include <iostream>
#include <string>

namespace greetings {
std::string hello(const std::string& name) {
    return "Hello, " + name;
}
}

int main() {
    using greetings::hello;

    std::string name = "Grace";

    std::string message = hello(name);

    std::cout << "name=" << name << std::endl;
    std::cout << "message=" << message << std::endl;
    return 0;
}
  1. name ← Ada

    10int main() {11    using greetings::hello;1213    std::string name→ Ada = "Ada"; //@name="Bjarne", "Grace"1415    std::string message = hello(nameAda);
  2. std::string hello(const std::string& name)

    4namespace greetings {5std::string hello(const std::string& nameAda) {6    return "Hello, " + nameAda;7}
  3. message ← Hello, Ada

    15    std::string message→ Hello, Ada = hello(nameAda);1617    std::cout << "name=" << nameAda << std::endl;18    std::cout << "message=" << messageHello, Ada << std::endl;19    return 0;20}
    outputname=Ada
    message=Hello, Ada
  1. name ← Bjarne

    10int main() {11    using greetings::hello;1213    std::string name→ Bjarne = "Bjarne";1415    std::string message = hello(nameBjarne);
  2. std::string hello(const std::string& name)

    4namespace greetings {5std::string hello(const std::string& nameBjarne) {6    return "Hello, " + nameBjarne;7}
  3. message ← Hello, Bjarne

    15    std::string message→ Hello, Bjarne = hello(nameBjarne);1617    std::cout << "name=" << nameBjarne << std::endl;18    std::cout << "message=" << messageHello, Bjarne << std::endl;19    return 0;20}
    outputname=Bjarne
    message=Hello, Bjarne
  1. name ← Grace

    10int main() {11    using greetings::hello;1213    std::string name→ Grace = "Grace";1415    std::string message = hello(nameGrace);
  2. std::string hello(const std::string& name)

    4namespace greetings {5std::string hello(const std::string& nameGrace) {6    return "Hello, " + nameGrace;7}
  3. message ← Hello, Grace

    15    std::string message→ Hello, Grace = hello(nameGrace);1617    std::cout << "name=" << nameGrace << std::endl;18    std::cout << "message=" << messageHello, Grace << std::endl;19    return 0;20}
    outputname=Grace
    message=Hello, Grace