Eclipse CDT 索引和 std::unique_ptr

发布于 2024-12-27 06:37:09 字数 746 浏览 1 评论 0原文

我在这段代码中使用 std::unique_ptr ,它按我的预期编译和运行。

std::stringstream out;
out << std::setw(3) << std::setfill('0') << i;
std::unique_ptr<std::string> s(new std::string(out.str()));
s->insert(s->end()-2, 1, '.');
return std::move(s);

但是,我从 Eclipse CDT 收到错误消息。第四行:无法解析方法“insert”,无法解析方法“end”。

以前,我在名称 std::unique_ptr 的出现上也遇到错误。这是通过设置预处理器符号 __GXX_EXPERIMENTAL_CXX0X__ 并重建索引来解决的,如 这个问题。

有没有办法让 CDT 理解 s 的类型为 std::string * 并且它应该在 std::string 中查找 s->insert() 和 s->end() ?

PS:我正在使用 Eclipse 3.7.1 和 CDT 8.0.0.201106081058

PS2:我想将其作为上述问题中的评论发布,但我不能,大概是因为我是新用户

I am using std::unique_ptr in this piece of code which compiles and runs as I expected.

std::stringstream out;
out << std::setw(3) << std::setfill('0') << i;
std::unique_ptr<std::string> s(new std::string(out.str()));
s->insert(s->end()-2, 1, '.');
return std::move(s);

However, I am getting error messages from Eclipse CDT. At the fourth line: Method 'insert' could not be resolved, Method 'end' could not be resolved.

Previously, I was also getting errors on appearances of the name std::unique_ptr. This was solved by setting the pre-processor symbol __GXX_EXPERIMENTAL_CXX0X__ and rebuilding the index, as described in the answer to this question.

Is there a way to make CDT understand that s is of type std::string * and that it should look in std::string for s->insert() and s->end() ?

PS: I am using Eclipse 3.7.1 and CDT 8.0.0.201106081058

PS2: I would have liked to post this as a comment in the above question, but I can't, presumably because I am a new user

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么这么可爱啊 2025-01-03 06:37:09

Eclipse CDT 索引器似乎无法推导出 unique_ptr::pointer 类型,该类型也用作运算符->() 的返回类型。当您键入类似

std::unique_ptr<Type *> ptr;
ptr.reset(new Type);

错误时,您会看到这一点,将“检测到”没有匹配的重载,并且唯一的候选者将是 reset(?)。所以这显然是一个错误。

It seems as if the Eclipse CDT indexer is not able to deduce the unique_ptr::pointer type that is also used as the return type of operator->(). You can see this when you type something like

std::unique_ptr<Type *> ptr;
ptr.reset(new Type);

an error will be "detected" that there would be no matching overload, and that the only candidate would be reset(?). So this is obviously a bug.

一刻暧昧 2025-01-03 06:37:09

此问题最近已在 cdt 8.1.1 中修复。只需转到“帮助”->“检查更新”即可下载并安装。我已经测试了 unique_ptr 并且它已正确索引。

This issue has been recently fixed, in cdt 8.1.1. Just go help->check for updates and it will be downloaded and installed. I've tested unique_ptr and it is properly indexed.

倾其所爱 2025-01-03 06:37:09

我在较新版本的 Eclipse CDT (9.3) 上也遇到同样的问题。我尝试了在互联网上找到的所有技巧,每次重建索引,希望有所改变。但索引器始终无法推断出 std::unique_ptr::operator->() 的类型。最后,我决定使用一个非常简单的解决方法:

# ifdef ECLIPSE_INDEXER_WORKAROUND
MyType* my_var;
# else
std::unique_ptr<MyType> my_var;
# endif    

我将 ECLIPSE_INDEXER_WORKAROUND 添加到 Eclipse 中的预处理器符号(当然仅用于索引选项,而不是构建),索引再次有用!

为了减少对代码的污染,我们可以使用宏:

# ifdef ECLIPSE_INDEXER_WORKAROUND
#  define MY_UNIQUE_PTR( type ) type* 
# else
#  define MY_UNIQUE_PTR( type ) std::unique_ptr< type >
# endif

MY_UNIQUE_PTR( int ) pint{ new int(42) };

I have the same issue on newer version of Eclipse CDT (9.3). I tried all the tricks I found on the internet, rebuilding every time my index, hoping for a change. But the indexer was never able to deduce the type of std::unique_ptr<T>::operator->(). Finally, I decided to use a very simple workaround:

# ifdef ECLIPSE_INDEXER_WORKAROUND
MyType* my_var;
# else
std::unique_ptr<MyType> my_var;
# endif    

I add ECLIPSE_INDEXER_WORKAROUND to preprocessor symbols (of course for indexing options only, not building) in Eclipse, and indexing is useful again!

In order to pollute the code less, we can use a macro:

# ifdef ECLIPSE_INDEXER_WORKAROUND
#  define MY_UNIQUE_PTR( type ) type* 
# else
#  define MY_UNIQUE_PTR( type ) std::unique_ptr< type >
# endif

MY_UNIQUE_PTR( int ) pint{ new int(42) };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文