Netbeans 8.2代码协助问题与unique_ptr(不是shared_ptr) [英] Netbeans 8.2 code assistance issue with unique_ptr (not shared_ptr)

查看:88
本文介绍了Netbeans 8.2代码协助问题与unique_ptr(不是shared_ptr)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux和GCC 8.1上使用Netbeans 8.2时,unique_ptr::operator->()发出错误警告 Unable to resolve template based identifier {var},更重要的是不会自动完成成员名称.

Using Netbeans 8.2 on Linux and GCC 8.1, unique_ptr::operator->() gives an erroneous warning Unable to resolve template based identifier {var} and more importantly will not autocomplete member names.

代码可以编译并正常运行,而且令人惊讶的是,如果使用shared_ptr代替,则自动完成功能仍会在没有警告的情况下运行.我不知道这怎么可能.这是一个有问题的示例,仅供参考

Code compiles and runs just fine, and amazingly, the autocomplete still works with no warnings if a shared_ptr is used instead. I have no idea how that is possible. Here is a problematic example, for reference

#include <iostream>
#include <memory>

struct C { int a;};

int main () {
  std::unique_ptr<C> foo (new C);
  std::shared_ptr<C> bar (new C);

  foo->a = 10; //Displays warning, does not auto-complete the members of C
  bar->a = 20; //No warning, auto-completes members of C

  return 0;
}

我已经尝试通过以下方法来解决此问题,但是没有运气:

I've tried the following to resolve this issue with no luck:

  1. 代码协助>修复项目
  2. 代码帮助>清理C/C ++缓存并重新启动IDE
  3. 手动删除~/.cache/netbeans/8.2
  4. 中的缓存
  5. 通过View> IDE Log查找可能有用的任何内容
  6. 在项目属性中将C和C ++编译器都设置为C11和C ++ 11
  7. 将预处理宏__cplusplus更改为201103L和201402L
  8. 创建一个新的Netbeans项目并尝试上述操作
  9. 上述选项的排列顺序各不相同
  1. Code Assistance > Reparse Project
  2. Code Assistance > Clean C/C++ cache and restart IDE
  3. Manually deleting cache in ~/.cache/netbeans/8.2
  4. Looking through View > IDE Log for anything that may help
  5. Setting both the C and C++ compilers to C11 and C++11 in project properties
  6. Changing the pre-processing macro __cplusplus to both 201103L and 201402L
  7. Creating a new Netbeans project and trying the above
  8. A large variety of permutations of the above options in different orders

同样,一切都可以编译并正常运行,只是代码帮助给了我一个问题.我用光了其他堆栈溢出答案中发现的内容.我的直觉告诉我shared_ptr有效和unique_ptr不起作用是有帮助的,但是我对C ++的了解不足以利用这些信息.请帮助我,我需要恢复工作...

Again, everything compiles and runs just fine, it's just Code Assistance that is giving me an issue. I've run out of things that I've found in other stack overflow answers. My intuition tells me that shared_ptr working and unique_ptr not working is helpful, but I don't know enough about C++ to utilize that information. Please help me, I need to get back to work...

编辑1

This stack overflow question, although referencing Clang and the libc++ implementation, suggests that it may be an implementation issue within GCC 8.1's libstdc++ unique_ptr.

推荐答案

TLDR方法1

在结构中添加using pointer = {structName}*指令可修复代码帮助,并将按预期进行编译和运行,如下所示:

Add a using pointer = {structName}* directive in your structure fixes code assistance, and will compile and run as intended, like so:

struct C { using pointer = C*; int a;};

TLDR方法2

The answer referenced in my edit does in fact work for libstdc++ as well. Simply changing the return type of unique_ptr::operator->() from pointer to element_type* will fix code assistance, compile and run as expected (the same thing can be done to unique_ptr::get()). However, changing things in implementations of the standard library worries me greatly.

更多信息

作为一个刚接触c ++并几乎不了解模板专业化功能的人,通读unique_ptr.h让人感到恐惧,但我认为这是困扰Netbeans的原因:

As someone who is relatively new to c++ and barely understands the power of template specializations, reading through unique_ptr.h was scary, but here is what I think is messing up Netbeans:

  1. 呼叫unique_ptr::operator->()呼叫unique_ptr::get()
  2. unique_ptr::get()调用私有实现的(__unique_ptr_impl)指针函数__unique_ptr_impl::_M_ptr().所有这些调用均返回__unique_ptr_impl::pointer类型.
  3. 在私有实现中,类型pointer是在更私有的实现_Ptr中定义的. struct _Ptr有两个模板定义,一个返回原始指针指向unique_ptr的原始指针,第二个似乎删除该模板变量的所有引用,然后找到名为pointer的类型.我认为这就是Netbeans搞砸的地方.
  1. Calling unique_ptr::operator->() calls unique_ptr::get()
  2. unique_ptr::get() calls the private implementation's (__unique_ptr_impl) pointer function __unique_ptr_impl::_M_ptr(). All these calls return the __unique_ptr_impl::pointer type.
  3. Within the private implementation, the type pointer is defined within an even more private implementation _Ptr. The struct _Ptr has two template definitions, one that returns a raw pointer to the initial template variable of unique_ptr, and the second that seems to strip any reference off this template variable, and then find it's type named pointer. I think that this is where Netbeans messes up.

所以我的理解是,当您调用unique_ptr<elementType, deleterType>::operator->()时,它会转到__unique_ptr_impl<elementType, deleterType>,其中通过删除所有引用的elementType并获取名为dereferenced(elementType)::pointer的类型来找到内部指针类型.因此,通过包含指令using pointer =,Netbeans可以找到dereferenced(elementType)::pointer类型所需的内容.

So my understanding is when you call unique_ptr<elementType, deleterType>::operator->(), it goes to __unique_ptr_impl<elementType, deleterType>, where the internal pointer type is found by stripping elementType of any references and then getting the type named dereferenced(elementType)::pointer. So by including the directive using pointer =, Netbeans gets what it wants in finding the dereferenced(elementType)::pointer type.

包含using指令完全是肤浅的,事实是如果没有该指令,一切都会编译,并通过以下示例证明

Including the using directive is entirely superficial, evidenced by the fact that things will compile without it, and by the following example

#include <memory>
#include <iostream>
struct A{
  using pointer = A*; 
  double memA; 
  A() : memA(1) {}
};
struct B {
  using pointer = A*; 
  double memB; 
  B() : memB(2) {}
};
int main() {
  unique_ptr<A> pa(new A);
  unique_ptr<B> pb(new B);
  std::cout << pa->memA << std::endl;
  std::cout << pb->memB << std::endl;
};

输出

1
2

应有的,即使结构B中包含using pointer = A*. Netbeans实际上试图自动完成B->B->memA,这进一步证明了Netbeans正在使用上面提出的逻辑.

As it should, even though in struct B contains using pointer = A*. Netbeans actually tries to autocomplete B-> to B->memA, further evidence that Netbeans is using the logic proposed above.

该解决方案被设计为万劫不复,但至少可以在不更改stl的实现的情况下起作用(在我使用过的非常具体的上下文中).谁知道,我仍然对unique_ptr中复杂的打字系统感到困惑.

This solution is contrived as all hell but at least it works (in the wildly specific context that I've used it) without making changes to implementations of stl. Who knows, I'm still confused about the convoluted typing system within unique_ptr.

这篇关于Netbeans 8.2代码协助问题与unique_ptr(不是shared_ptr)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆