gpt4 book ai didi

c++ - 与类型同名的类型别名

转载 作者:行者123 更新时间:2023-12-01 13:07:39 25 4
gpt4 key购买 nike

它是有效的 C++ 吗?

#include <iostream>


class Test {

struct Inner {
};

public:
using Inner = struct Inner; // Alias with same name as type
};

int main(int argc, const char * argv[]) {
static_assert(std::is_pod<Test::Inner>::value, "");

return 0;
}
使用 clang 编译但不能使用 GCC/Visual C++ 编译(“Inner is private ...”错误消息)

最佳答案

GCC 和 Visual C++ 是正确的。

确实可以使用 using实际上更改成员的访问权限,例如

using Inner_ = Inner;


static_assert(std::is_pod<Test::Inner_>::value, "");

在函数中。

但是在类型别名与成员同名的情况下,C++ 要求范围解析运算符查找成员。所以在你的情况下 Test::Inner指实际成员而不是 using因此编译应该失败,因为它是 private .

https://en.cppreference.com/w/cpp/language/qualified_lookup , 特别是

Qualified lookup within the scope of a namespace N first considers all declarations that are located in N and all declarations that are located in the inline namespace members of N (and, transitively, in their inline namespace members). If there are no declarations in that set then it considers declarations in all namespaces named by using-directives found in N and in all transitive inline namespace members of N

关于c++ - 与类型同名的类型别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61391927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com