简体   繁体   English

在类中定义的友元函数模板是否可用于查找? clang ++和g ++不同意

[英]Is a friend function template defined in the class available for lookup? clang++ and g++ disagree

Here is the code: 这是代码:

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 gives: clang ++ 3.4给出:

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 compiles it, but I don't think it should have. g ++ 4.9.0编译它,但我不认为它应该有。 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. 第15.4.2 / 2,4节讨论了这一点,但他们都没有说任何暗示在类中定义的友元函数模板应该与类中定义的非模板友元函数具有不同的可见性。

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. 这只是我的学术兴趣,虽然它确实来自其他可能有实际用例的人提出的问题。

It looks like a g++ bug to me. 它看起来像是一个g ++错误。

Yes, this is an error. 是的,这是一个错误。 I'm surprised it's finding the function. 我很惊讶它找到了这个功能。 Apparently GCC fails entirely to hide function templates. 显然GCC完全没有隐藏功能模板。

This C++03 example also compiles, so it could be a regression: 这个C ++ 03示例也编译,所以它可能是一个回归:

struct foo {
  template<typename T >
  friend void f( T ) { }

};

int main() {
  f( 3 ); // clang++ can't find it, g++ can.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 g ++和clang ++在模板类中定义的朋友模板函数的不同行为 - g++ and clang++ different behaviour with friend template function defined inside a template class 编译模板类因g ++或clang ++失败 - Compile template class failed by g++ or clang++ C ++朋友函数模板重载和SFINAE在clang ++,g ++,vc ++中的不同行为(C ++ 14模式) - C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode) G ++,clang ++和std :: function - G++, clang++ and std::function C ++ 11构造函数重载解析和Initialiser_lists:clang ++和g ++意见不同 - C++11 constructor overload resolution and initialiser_lists: clang++ and g++ disagree clang / g ++与朋友功能的区别 - clang/g++ difference with friend function 使用clang ++和g ++,双嵌套模板类中的c ++ static int def失败 - c++ static int def in doubly nested template class fails with clang++ and g++ 这是 g++ 或 clang++ 中的错误 - Is this a bug in g++ or clang++ g ++和clang ++不同的行为推断函数的模板返回类型 - g++ and clang++ different behaviour inferring the template return type of a function 具有已删除“一般”情况的专用模板功能无法使用g ++ &lt;= 4.8.0和clang ++进行编译 - Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM