redefinition compiler errors for anonymous namespace constants

Hi,

I’m porting some of my older code from UE4.24 to UE5.3 but I’m facing a weird problem. I like to use unnamed namespaces for constants that are only be meant to be used in the local file.
But for some reason I’m getting redefinition compiler errors in UE5.

1>ClassA.cpp(14): error C2374: 'MyNamespace::`anonymous-namespace'::maxLength': redefinition; multiple initialization
1>ClassB.cpp(10): note: see declaration of 'MyNamespace::`anonymous-namespace'::maxLength'

The whole reason for anonymous namespaces is to limit the scope and to avoid redefinition conflicts. The error is not showing up in VS native C++ projects and in my Unreal 4 projects so it must be Unreal 5 connected. Anyone an idea what’s going on here?

Cheers

I guess this happens because UE uses unity builds. Several .cpp files are combined in single file and then this file is compiling. In this case all anonymous namespaces from these files are combined and redefinition happens. Try using constexpr if possible.