【发布时间】:2019-11-10 21:28:56
【问题描述】:
我想知道为什么 following code 会编译。
#include <iostream>
template<class T>
void print(T t) {
std::cout << t;
}
namespace ns {
struct A {};
}
std::ostream& operator<<(std::ostream& out, ns::A) {
return out << "hi!";
}
int main() {
print(ns::A{});
}
我的印象是,在实例化点,不合格的依赖名称只能通过 ADL 进行查找 - 这不应该考虑全局命名空间。我错了吗?
【问题讨论】:
-
Clang 拒绝编译这个,正如你所料:
error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup。可能是 GCC 错误? -
@HolyBlackCat 可能甚至不是一个错误,因为几乎任何带有模板的错误都是 UB(可悲)。
-
@curiousguy UB 到底是什么?
-
@IgorR。任何时候在模板中查找在不同点找到另一个名称并重载选择它时,它就是 UB。我想。情况很复杂。太复杂了!
标签: c++ templates language-lawyer argument-dependent-lookup name-lookup