使用 void_t 和保护巢 类 的基于 SFINAE 的检测

SFINAE-based detection using void_t and protected nest classes

我最近在 void_t 属性 检测和 protected/private class 信息方面遇到了 clang 和 gcc 之间的一些不同行为。考虑定义如下的类型特征:

#include <type_traits>

template<typename T, typename = void>
constexpr const bool has_nested_type_v = false;

template<typename T>
constexpr const bool has_nested_type_v
<T, std::void_t<typename T::type>> = true;

给定具有受保护或私有嵌套 type classes 的示例类型和一个简单的程序

#include "has_nested_type.hpp"
#include <iostream>

struct Protected {
protected:
  struct type{};
};

struct Private {
private:
  struct type{};
};

int main() {
  std::cout << "Protected: " 
            << (has_nested_type_v<Protected> ? "detected" : "not detected")
            << std::endl;
  std::cout << "Private: " 
            << (has_nested_type_v<Private> ? "detected" : "not detected")
            << std::endl;
}

GCC 为该程序发出以下错误。

prog.cc:16:21: error: 'struct Protected::type' is protected within this context
                 << (has_nested_type_v<Protected> ? "detected" : "not detected")
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:6:14: note: declared protected here
       struct type{};
              ^~~~
prog.cc:19:21: error: 'struct Private::type' is private within this context
                 << (has_nested_type_v<Private> ? "detected" : "not detected")
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:11:14: note: declared private here
       struct type{};
              ^~~~

我的问题是哪种行为符合标准?应该在此处发出 clang 错误并发出诊断信息,还是 GCC 过于急切?

这是一个 GCC bug. It's a part of the following meta-bug,描述了几个与访问相关的错误。