vendredi 30 décembre 2022

Why can I reference a member of an enum that is only forward declared

For example, given the following:

// enum.h
enum class TestEnum: int {
    ONE,
    TWO,
    THREE
};
// function.h
enum class TestEnum: int;
int TestFunction(TestEnum te = TestEnum::THREE);
// function.cpp
#include "enum.h"
#include "function.h"

int TestFunction(TestEnum te) { 
    return 5;
}

In my eyes this should not compile because in function.h, with TestEnum only being forward declared, the compiler cannot know that TestEnum::THREE (which is used as a default argument for TestFunction) is a valid member of the enum. And yet it does.

If I forward declare a class, I cannot access its members, but with enums it seems I can. Why is that? And is this something "legal" as per the standard, or did it just happen to work because of the way the compilers I tried (clang and gcc) are implemented?

Aucun commentaire:

Enregistrer un commentaire