作者:Susan林小夕 | 来源:互联网 | 2022-12-03 09:16
我有一些代码使用模板转换运算符来查找通过ADL找到的函数的返回类型.
简化的代码如下所示:
#include
template
struct probe {
template::value &&
!std::is_const::value, int> = 0>
operator T& ();
template::value &&
!std::is_const::value, int> = 0>
operator T&& ();
template::value, int> = 0>
operator T const& () const;
template::value, int> = 0>
operator T const&& () const;
};
namespace foo {
struct bar {};
auto find_me(bar const&) -> int { return 0; }
}
int main() {
// That would be inside a template in my code.
find_me(probe{});
}
在clang 6和GCC中,上面的代码编译.但是,在Clang 7中,它不再编译了!
https://godbolt.org/z/Lfs3UH
正如你所看到的,clang 6解决了对call的调用,probe::operator foo::bar&&()
但是clang 7因为尝试调用而失败probe::operator const foo::bar&&()
哪个编译器是对的?标准中的规则是什么?这是一个新的Clang错误还是修复?
我想检查很多情况.不只是foo::bar
作为参数,而是许多引用类型,例如:
namespace foo {
struct bar {};
auto find_me(bar const&) -> int { return 0; }
auto find_me(bar&&) -> int { return 0; }
auto find_me(bar const&&) -> int { return 0; }
auto find_me(bar&) -> int { return 0; }
}
int main() {
find_me(probe{});
find_me(probe{});
find_me(probe{});
find_me(probe{});
find_me(probe{});
}
解决正确的函数调用很重要.
以下是所有这些案例的实例,GCC成功但clang失败:https://godbolt.org/z/yrDFMg