Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Using declaration

A using declaration makes a selected name available in the current scope. It can shorten references to namespace names and restore base-class overloads hidden by a declaration in a derived class.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

using namespace_name::name;
using Base::member;
using Base::Base;
using typename Base::type_name;

using N::name; makes existing declarations available as name in the current scope without copying an object. It can appear at namespace or block scope; in a class, its uses include exposing base-class members.

Do not confuse it with the directive using namespace N; or the type alias using Name = Type;. A declaration selects a particular name, but for functions it can name an overload set. It does not automatically include overloads declared later in the namespace.

In the example, using Base::select; keeps the int overload available alongside the double version. Without that declaration, Derived::select hides the base-class name, so both calls select the double version.

using Base::Base; enables inheriting constructors from a direct base class. using typename Base::type_name; identifies a dependent type name in a template. The declaration does not bypass access checks; name conflicts can cause errors or ambiguity.

Reference identifiers covered by this entry: using declaration.

Examples

#include <iostream>

namespace values {
    constexpr int answer = 42;
}

struct Base {
    const char* select(int) const { return "Base::select(int)"; }
};

struct Derived : Base {
    using Base::select;
    const char* select(double) const { return "Derived::select(double)"; }
};

int main() {
    using values::answer;
    Derived object;
    std::cout << answer << '\n';
    std::cout << object.select(1) << '\n';
    std::cout << object.select(1.5) << '\n';
}

Result

42
Base::select(int)
Derived::select(double)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources