是否在类中定义的一个友好函数模板可用于查找? clang ++和g ++不同意 [英] Is a friend function template defined in the class available for lookup? clang++ and g++ disagree
问题描述
这里是代码:
struct foo {
template<typename T = void>
friend foo f() { return {}; }
};
int main() {
auto x = f(); // clang++ can't find it, g++ can.
}
clang ++ 3.4提供:
clang++ 3.4 gives:
fni2.cpp:8:12: error: use of undeclared identifier 'f'
auto x = f(); // clang++ can't find it, g++ can.
^
1 error generated.
g ++ 4.9.0编译它,但我不认为它应该有。 这是一个相关的问题,但没有确定的答案。第15.4.2节/2.4讨论了这一点,但是他们都没有说什么建议在类中定义的朋友函数模板应该与类中定义的非模板朋友函数具有不同的可见性。
g++ 4.9.0 compiles it, but I don't think it should have. This is a related question, but there was no definitive answer. Section 15.4.2/2,4 discuss this, but neither of them say anything to suggest that friend function templates defined in-class should have different visibility from non-template friend functions defined in-class.
这只是我的学术兴趣,虽然它确实来自可能有实际用例的其他人的问题。
This is of academic interest to me only, though it did arise from a question by someone else who may have had an actual use case.
它看起来像一个g ++ bug给我。
It looks like a g++ bug to me.
推荐答案
是的,这是一个错误。我很惊讶,它找到的功能。显然GCC不完全隐藏函数模板。
Yes, this is an error. I'm surprised it's finding the function. Apparently GCC fails entirely to hide function templates.
这个C ++ 03的例子也编译,所以它可以是一个回归:
This C++03 example also compiles, so it could be a regression:
struct foo {
template<typename T >
friend void f( T ) { }
};
int main() {
f( 3 ); // clang++ can't find it, g++ can.
}
这篇关于是否在类中定义的一个友好函数模板可用于查找? clang ++和g ++不同意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!