According to Can SFINAE detect private access violations?, SFINAE should detect private access violations. However, the following example which checks whether a class includes a member passes even if that member should be inaccessible (as it is declared as private) using Visual C++ 2022:
#include <type_traits>
template<typename, typename = void>
struct has_member_foo : std::false_type{};
template<typename T>
struct has_member_foo<T, std::void_t<decltype(T::foo_)>> : std::true_type {};
class TestWithPrivateFooMember
{
private:
int foo_;
};
int main()
{
// Should be false but evaluates to true with Visual Studio 2022 and compiles just fine...
static_assert(has_member_foo<TestWithPrivateFooMember>::value);
}
// Compiled with Visual C++ 2022 using the command line:
// cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 -std:c++20 /FS -c main.cpp
Could it be a Visual C++ bug?
The static assertion should raise a compilation error.