我进行了搜索以查看是否找到了解决此问题的方法,但没有找到答案。我遇到的问题是在我的代码编译时,我没有得到 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));
}
最佳答案
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 does not support range-based for .不过它确实支持 lambda:
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++ - IntelliSense Visual Studio 2010 SP1 unique_ptr : unique_ptr<vector<unique_ptr<T>>> T,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11475785/