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)
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过于急切?
| 归档时间: |
|
| 查看次数: |
211 次 |
| 最近记录: |