我想实现一个简单的 native C++ 固定容量数组模板类,为了方便起见支持基于范围的“for each”语法,开销最小。
我在 const 实例上支持它时遇到问题。
有了这个实现:
template< class T, size_t Capacity >
class List
{
public:
List() { mSize = 0; }
const T* begin() const { return mItems; }
const T* end() const { return mItems + mSize; }
T* begin() { return mItems; }
T* end() { return mItems + mSize; }
private:
size_t mSize;
T mItems[ Capacity ];
};
和这种用法:
const List< int, 5 > myInts;
for each( const int myInt in myInts )
{
continue;
}
我收到这个错误:
error C2440: 'initializing' : cannot convert from 'const int *' to 'int *'
Conversion loses qualifiers
这种用法不会提示:
List< int, 5 > myInts;
for each( const int myInt in myInts )
{
continue;
}
而且这个(不受欢迎的)实现不会提示:
template< class T, size_t Capacity >
class List
{
public:
List() { mSize = 0; }
T* begin() const { return const_cast< List* >( this )->mItems; }
T* end() const { return const_cast< List* >( this )->mItems + mSize; }
private:
size_t mSize;
T mItems[ Capacity ];
};
背后到底发生了什么我不明白的事? std::vector<> 正确处理这个问题的原因是什么?谢谢!
最佳答案
你的用例对我来说似乎有点奇怪,因为你写下来的 C++ 中没有 for each constructs。 C++11 中引入了常规的 for 和基于范围的 for。我只能猜测您的真实用例是什么,但很可能编译器会因 const-correctness 错误而提示。如果没有您尝试运行的真实代码,我无法真正准确地确定您的错误。无论如何,下面是一个演示这两种用法的工作示例。希望对您有所帮助,但如果您有任何疑问,请随时跟进,我会尽力解释。
#include <cstdlib>
#include <iostream>
template <typename T, std::size_t Capacity>
class List {
public:
List() : mSize(0) {}
const T *begin() const { return mItems; }
const T *end() const { return mItems + mSize; }
T *begin() { return mItems; }
T *end() { return mItems + mSize; }
void add(int v)
{
// TODO: Check for out of range here...
mItems[mSize++] = v;
}
private:
size_t mSize;
T mItems[Capacity];
};
int main()
{
/* const */ List<int, 10> array;
array.add(1);
array.add(11);
array.add(15);
array.add(3);
// C++11 style (range-based for)
for (int p : array) {
std::cout << p << '\n';
}
// Pre C++11 style
for (const int *from = array.begin(), *to = array.end(); from != to; ++from)
{
int p = *from;
std::cout << p << '\n';
}
}
关于c++ - 在自定义 const native C++ 容器类上支持 "for each",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13794995/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que