This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
Summary: | Free function friend cannot access private members when templated | ||
---|---|---|---|
Product: | cnd | Reporter: | konradsa |
Component: | Code Model | Assignee: | petrk |
Status: | RESOLVED INVALID | ||
Severity: | normal | ||
Priority: | P3 | ||
Version: | 7.4 | ||
Hardware: | Other | ||
OS: | Linux | ||
Issue Type: | DEFECT | Exception Reporter: |
Description
konradsa
2014-02-28 01:47:11 UTC
Still broken in 8.0 RC1, just tried it out there too. Sorry, it seems that this code is not correct. If you want to declare non-template friend operator, you have to define non-template operator as well. And vice versa if you declare template function, it cannot be defined by non-template function. In fact your example cannot be built by gcc 4.8.1 (if you will use that operator, otherwise you do not need to have definition of it). Note that not just compiled as compilation phase doesn't include resolving of external references. To make it more clear: 1) If you will add that code to you sample (and fix forward and friend declarations of 'B'), you won't be able to run it, because linker won't find definition of friend operator '<<' ========================================== int main() { A<int> cls; std::cout << cls << std::endl; return 0; } ========================================== 2) To fix that you can modify friend operator declaration in the way below. In that case NetBeans resolves private variable. ========================================== template<class U> friend std::ostream& operator<<(std::ostream& out, const A<U>& rhs); ========================================== 3) If you want to keep it non-templated, you have to define non-templated operator. In this case for each instantiation of 'A' which is used with std::ostream, you have to provide separate definition of friend operator: ========================================== std::ostream& operator<<(std::ostream& out, const A<int>& rhs) { // NOTE 'int' HERE std::cout << rhs.var << std::endl; // parser error here } ========================================== |