这个名称解析如何与实现函数和回退函数一起工作?
How does this name resolution work with an implementator function and a fallback function?
我正在进行的一个项目需要编译多个目标。每个目标的底层实现可能会有所不同,因为设备需要不同的硬件配置。
为了强制目标实现遵循 interface/design 设计了合同系统。如果目标没有相应地实现所述接口,则会在使用时抛出错误。
以下代码使用gcc、arm-none-eabi-gcc和clang进行测试
namespace A {
namespace C {
void foo() {}
}
}
namespace B {
using namespace A::C;
void foo() {}
}
using namespace A;
namespace C {
}
int main() {
B::foo(); // ok
C::foo(); // won't compile
return 0;
}
现在在推理这段代码编译或不编译的原因时出现了多个问题:
为什么编译器不报告 A::foo(bool) 和 B::set(bool) 之间未解决的歧义?
为什么 C::foo() 不能 编译,因为我的理论是实现了相同的命名结构,但方式不同:
为什么编译器不报告 target::set(bool) 和 interface_contracts::set(bool) 之间未解决的歧义?
在第一个代码片段中,name hwstl::target::pin::set
隐藏了 name hwstl::interface_contracts::pin::set
。
对于调用 hwstl::device::pin::set(true);
,名称查找在找到 hwstl::target::pin::set
后停止。只有一个候选函数,没有歧义。
对于调用hwstl::unsatisfied_device::pin::set(true);
,只有一个名为set
的函数,无论如何都可以找到。
10.3.4.1 A using-directive does not add any members to the declarative region in which it appears.
为什么下面的代码编译不通过?
在第二个代码片段中,您通过合格的 ID 调用 set
:hwstl::unsatisfied_device::pin::set
,编译器将仅尝试在命名空间 hwstl::unsatisfied_device::pin
中查找名称。因此,它无法在其外部找到 using 指令 using namespace interface_contracts;
引入的名称。
这是您的代码的简化版本:
namespace A {
void foo() {}
}
namespace B {
using namespace A;
void foo() {}
}
using namespace A;
namespace C {
}
int main() {
B::foo(); // ok
C::foo(); // won't compile
return 0;
}
我正在进行的一个项目需要编译多个目标。每个目标的底层实现可能会有所不同,因为设备需要不同的硬件配置。
为了强制目标实现遵循 interface/design 设计了合同系统。如果目标没有相应地实现所述接口,则会在使用时抛出错误。
以下代码使用gcc、arm-none-eabi-gcc和clang进行测试
namespace A {
namespace C {
void foo() {}
}
}
namespace B {
using namespace A::C;
void foo() {}
}
using namespace A;
namespace C {
}
int main() {
B::foo(); // ok
C::foo(); // won't compile
return 0;
}
现在在推理这段代码编译或不编译的原因时出现了多个问题:
为什么编译器不报告 A::foo(bool) 和 B::set(bool) 之间未解决的歧义?
为什么 C::foo() 不能 编译,因为我的理论是实现了相同的命名结构,但方式不同:
为什么编译器不报告 target::set(bool) 和 interface_contracts::set(bool) 之间未解决的歧义?
在第一个代码片段中,name hwstl::target::pin::set
隐藏了 name hwstl::interface_contracts::pin::set
。
对于调用 hwstl::device::pin::set(true);
,名称查找在找到 hwstl::target::pin::set
后停止。只有一个候选函数,没有歧义。
对于调用hwstl::unsatisfied_device::pin::set(true);
,只有一个名为set
的函数,无论如何都可以找到。
10.3.4.1 A using-directive does not add any members to the declarative region in which it appears.
为什么下面的代码编译不通过?
在第二个代码片段中,您通过合格的 ID 调用 set
:hwstl::unsatisfied_device::pin::set
,编译器将仅尝试在命名空间 hwstl::unsatisfied_device::pin
中查找名称。因此,它无法在其外部找到 using 指令 using namespace interface_contracts;
引入的名称。
这是您的代码的简化版本:
namespace A {
void foo() {}
}
namespace B {
using namespace A;
void foo() {}
}
using namespace A;
namespace C {
}
int main() {
B::foo(); // ok
C::foo(); // won't compile
return 0;
}