我最近在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;
给出具有受保护或私有嵌套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;
}
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{};
^~~~
我的问题是哪种行为符合标准?应该在这里出现错误并发出诊断信息吗?还是GCC过于急于求成?