使用void_t和受保护的嵌套类进行基于SFINAE的检测

apm*_*ney 7 c++ gcc detection c++17 void-t

我最近在clang和gcc之间遇到了关于void_t属性检测和受保护/私有类信息的一些不同行为.考虑一个类型特征,定义如下:

#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;
Run Code Online (Sandbox Code Playgroud)

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

#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;
}
Run Code Online (Sandbox Code Playgroud)
  • clang成功编译失败的检测(如预期的那样).该程序,编译和输出上再现wandbox 这里.

  • gcc无法编译,发出诊断信息.这个错误可以wandbox再现这里.

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{};
              ^~~~
Run Code Online (Sandbox Code Playgroud)

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

Igo*_* R. 4

这是一个海湾合作委员会错误它是以下元错误的一部分,描述了几个与访问相关的错误。