我搜索了一下,看看我是否找到了解决这个问题的方法,但我无法看到答案。 我遇到的问题是在我的代码编译时,我没有得到intellisense
如果我正在使用模板T接收参数(或声明变量),例如:
unique_ptr<vector<unique_ptr<T>>> & dataSets;intellisense找到dataSets.get()但它找不到dataSets.get() - > clear(); 但是,如果我这样做,它会编译好。 但是,如果它不是模板,它似乎工作正常。
码:
template <typename T> void mtsql::MTMySQL<T>::executePrepareStatement(const string & sqlText,const unique_ptr<vector<SQLDataType>> & argList,unique_ptr<vector<unique_ptr<T>>> & dataSets) { dataSets.get()->clear(); unique_ptr<sql::ResultSet> rs; for (auto iter = argList->cbegin(); iter != argList->cend() ; ++iter) { auto ps = this->createPreparedStatment(sqlText,args); rs.reset(ps->execute()); dataSets.get()->insert(std::move(rs)); ps.release(); } }我是c ++ 11的新手,所以我可能会做一些可能错误的额外步骤或步骤(例如,我认为不需要ps.release()...我的意思是删除它,但是因为它是一个智能点)
谢谢您的帮助!
编辑1:感谢帮助,我的代码看起来更好,没有可能的泄漏。 谢谢!
dataSets->clear(); for (auto iter = argList->cbegin(); iter != argList->cend() ; ++iter) { auto ps = this->createPreparedStatment(sqlText,args); dataSets->push_back(std::move(rs)); }I searched to see if I find a solution to this problem but I was not able to see an answer. The problem that I'm having is while my code compiles, I don't get intellisense
if I'm receiving a parameter (or declaring a variable) such as this with template T :
unique_ptr<vector<unique_ptr<T>>> & dataSets;intellisense finds dataSets.get() but it does not find dataSets.get()->clear(); however, it compiles fine if I do it. However, if it not a template, it seems to work fine.
CODE:
template <typename T> void mtsql::MTMySQL<T>::executePrepareStatement(const string & sqlText,const unique_ptr<vector<SQLDataType>> & argList,unique_ptr<vector<unique_ptr<T>>> & dataSets) { dataSets.get()->clear(); unique_ptr<sql::ResultSet> rs; for (auto iter = argList->cbegin(); iter != argList->cend() ; ++iter) { auto ps = this->createPreparedStatment(sqlText,args); rs.reset(ps->execute()); dataSets.get()->insert(std::move(rs)); ps.release(); } }I'm new at c++11 so I may be doing extra steps or steps that may be wrong (for example, I think ps.release() is not needed... my point was to delete it, but since is a smart point)
Thanks for the help!
EDIT 1: Thanks to the help, my code looks much nicer and without possible leakage. Thank you!
dataSets->clear(); for (auto iter = argList->cbegin(); iter != argList->cend() ; ++iter) { auto ps = this->createPreparedStatment(sqlText,args); dataSets->push_back(std::move(rs)); }最满意答案
C ++不是一种解析和语义的简单语言。 因此,您无法期望IntelliSense能够与复杂类型完美配合。
至于你的代码,你可以简化代码来做:
dataSets->clear(); // no need to use `get` here. for (auto& ignored : *argList) { // use range-based for. auto ps = this->createPreparedStatment(sqlText,args); dataSets->insert(ps->execute()); // no need to define 'rs'. // no need to release explicitly, if they are indeed smart pointers. }(如果dataSets真的是unique_ptr<vector<unique_ptr<T>>>我认为你应该使用dataSets->push_back而不是insert 。)
编辑: MSVC 2010不支持基于范围的 。 它确实支持lambdas:
std::for_each(argList->cbegin(), argList->cend(), [&](const vector<SQLDataType>&) { auto ps = this->createPreparedStatment(sqlText,args); dataSets->insert(ps->execute()); // no need to define 'rs'. });C++ is not a simple language to parse and semantic. Therefore you can't expect IntelliSense to work perfectly with complicated types.
As for your code, you could simplify the code to do:
dataSets->clear(); // no need to use `get` here. for (auto& ignored : *argList) { // use range-based for. auto ps = this->createPreparedStatment(sqlText,args); dataSets->insert(ps->execute()); // no need to define 'rs'. // no need to release explicitly, if they are indeed smart pointers. }(And if dataSets is really a unique_ptr<vector<unique_ptr<T>>> I think you should use dataSets->push_back instead of insert.)
Edit: MSVC 2010 does not support range-based for. It does support lambdas though:
std::for_each(argList->cbegin(), argList->cend(), [&](const vector<SQLDataType>&) { auto ps = this->createPreparedStatment(sqlText,args); dataSets->insert(ps->execute()); // no need to define 'rs'. });更多推荐
发布评论