0Pricing
C++ Academy · Lesson

Range-based for nullptr and enum class

Use C++11 conveniences: range-based for, nullptr, and strongly typed enums.

Range-based for nullptr and enum class is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Three C++11 Quick Wins

C++11 brought many small improvements. Three you will use every day: range-based for, nullptr, and enum class.

Range-Based for Recap

Iterate any container that has begin() and end() without index arithmetic.

for (const auto& x : v) {
    process(x);
}

What It Replaces

The verbose iterator-based loop becomes a one-liner.

// Before C++11
for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
    process(*it);
}

nullptr Beats NULL

nullptr is a typed null pointer literal. Avoids the overload-resolution ambiguity of NULL (a macro for 0).

void f(int);
void f(int*);

f(NULL);      // calls f(int)
f(nullptr);   // calls f(int*)

Why nullptr Wins

Use nullptr consistently:

  • Distinct type (std::nullptr_t)
  • Clearer intent
  • Works in template metaprogramming
  • Cannot accidentally convert to int

Plain enum: A Footgun

Old-style enums leak their names into the enclosing scope and implicitly convert to int.

enum Color { RED, GREEN, BLUE };
int c = RED;            // implicit conversion, no warning
// RED, GREEN, BLUE leak into global scope

enum class: Scoped and Typed

C++11 enum class requires qualification and forbids implicit conversion to int.

enum class Color { Red, Green, Blue };
Color c = Color::Red;
// int n = Color::Red;     // ERROR
std::cout << static_cast<int>(c);   // explicit conversion

Specifying the Underlying Type

You can fix the storage size of an enum class to a specific integer type.

enum class Status : uint8_t { OK = 0, FAIL = 1, RETRY = 2 };

Forward-Declaring enum class

Because the underlying type is known, enum classes can be forward declared — handy for breaking header includes.

// header A
enum class Color : int;

// header B (full definition)
enum class Color : int { Red, Green, Blue };

When Plain enum Is OK

Plain enums still have uses: integer flag bitmasks where you really want implicit conversion. Document the design choice explicitly.

Migration Tip

When modernizing a codebase, convert plain enums to enum class gradually. Search for places that rely on implicit int conversion and add static_cast.

Quick Check

What makes enum class safer than a plain enum?

Recap

Three C++11 quick wins: range-based for cuts iterator boilerplate, nullptr replaces NULL, and enum class gives scoped, type-safe enums. Use all three by default.

Frequently asked questions

Is the “Range-based for nullptr and enum class” lesson free?

Yes — the full text of “Range-based for nullptr and enum class” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Range-based for nullptr and enum class”?

Use C++11 conveniences: range-based for, nullptr, and strongly typed enums. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Range-based for nullptr and enum class” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. auto decltype and Type Deduction
  2. Range-based for nullptr and enum class
  3. Lambda Expressions Capture Mutable Generic
  4. Structured Bindings and if-init C++17
← Back to C++ Academy