为什么这个最简单的 C++0x 代码无效?
我刚才遇到了一个奇怪的问题。
源代码简单且不言而喻,如下:
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
using namespace std::tr1;
template<class T_>
void show_size(T_ coll)
{
cout << coll.size();
}
int main()
{
vector<int> coll;
coll.push_back(1);
show_size(ref(coll));
return 0;
}
VC++ 2010 报告:
std::tr1::reference_wrapper<_Ty>'
error C2039: 'size' : is not a member of ' 要知道,reference_wrapper可以自动将自身转换为它的底层类型,这里是vector
。为什么这么简单的代码无效?
I encountered a weird problem just now.
The source code is simple and self-evident as follows:
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
using namespace std::tr1;
template<class T_>
void show_size(T_ coll)
{
cout << coll.size();
}
int main()
{
vector<int> coll;
coll.push_back(1);
show_size(ref(coll));
return 0;
}
The VC++ 2010 reports:
error C2039: 'size' : is not a member of 'std::tr1::reference_wrapper<_Ty>'
As we know, reference_wrapper can automatically convert itself to its underlying type, here is vector<int>
. Why is such simple code not valid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不能是参考包装器的全部要点,因为它不会从参考中衰减,除非使用
.get()
明确请求编辑:不要混淆 boosts 参考包装器与标准的相比,boost 实际上具有隐式转换(但目标功能有点不同)
No it can't that's the whole point of the reference wrapper, because it doesn't decay from the reference, unless explicitly requested using
.get()
Edit: don't mix up the boosts reference wrapper with the standard one, the boost one actually has implicit conversion (but the target functionality is a little bit different)