gpt4 book ai didi

c++ - 超出命名空间函数定义的参数类型查找

转载 作者:太空狗 更新时间:2023-10-29 21:32:53 24 4
gpt4 key购买 nike

我领导的软件项目偶尔会使用嵌套命名空间来指示库的哪些部分是内部的,不应被视为面向用户的。例如:

namespace project::detail {
// Hands off; library implementation internals
class Internal {};
int important_number(Internal internals);
}

在 C++17 之前,缩进会变得有些可怕(取决于源代码格式),因此我们选择在命名空间外定义函数:

int 
project::detail::important_number(Internal const internals)
{ ... }

请注意,虽然 Internal 类型是在 project::detail 嵌套命名空间中声明和定义的,但不需要完全限定参数类型(即:

int
project::detail::important_number(/*unnecessary*/ project::detail::Internal)

我搜索了en.cppreference.com和 C++14 标准的位,还没有找到支持缩写函数定义的地方(上面的第一个例子)。我假设 C++17 的相关规则没有改变。谁能告诉我 C++14/17 标准的哪些规则适用于此——即标准在哪里支持命名空间外函数定义的非完全限定参数类型?

最佳答案

您正在寻找非限定名称查找,它可以在basic.lookup.unqual 下的标准中找到。

In the definition of a function that is a member of namespace N, a name used after the function’s declarator-id shall be declared before its use in the block in which it is used or in one of its enclosing blocks or shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N’s enclosing namespaces.

关于c++ - 超出命名空间函数定义的参数类型查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53547218/

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