Ideone 测试用例:http://ideone.com/lzepF
代码:
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <stdint.h>
template <typename T> std::ostream& write(std::ostream& os, T const& x);
template <typename T> std::istream& read(std::istream& is, T& x);
template <typename T, typename U> std::ostream& write(std::ostream& os, std::pair<T, U> const& rh);
template <typename T, typename U> std::istream& read(std::istream& is, std::pair<T, U>& rh);
template <typename T> std::ostream& writeContainer(std::ostream& os, T const& rh);
template <typename T, typename U> std::ostream& write(std::ostream& os, std::map<T, U> const& rh);
template <typename T, typename U> std::istream& read(std::istream& is, std::map<T, U> rh);
template <typename T> std::ostream& write(std::ostream& os, std::vector<T> const& rh);
template <typename T> std::istream& read(std::istream& is, std::vector<T>& rh);
template <typename T>
std::ostream& write(std::ostream& os, T const& x){
static_assert(!std::is_pointer<T>(), "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod<T>(), "That's not a POD: can't write it");
os.write(reinterpret_cast<const char *>(&x), sizeof(T));
return os;
}
template <typename T>
std::istream& read(std::istream& is, T& x){
static_assert(!std::is_pointer<T>(), "That's a pointer, you probably don't want to read that");
static_assert(std::is_pod<T>(), "That's not a POD: can't read it");
is.read(reinterpret_cast<char *>(&x), sizeof(T));
return is;
}
template <typename T, typename U>
std::ostream& write(std::ostream& os, std::pair<T, U> const& rh){
write(os, rh.first);
write(os, rh.second);
return os;
}
template <typename T, typename U>
std::istream& read(std::istream& is, std::pair<T, U>& rh){
read(is, rh.first);
read(is, rh.second);
return is;
}
template <typename T>
std::ostream& writeContainer(std::ostream& os, T const& rh){
uint32_t size = std::distance(rh.begin(), rh.end());
write(os, size);
for(auto it = rh.begin(); it != rh.end(); ++it){
write(os, *it);
}
return os;
}
template <typename T>
std::istream& readContainer(std::istream& is, T& rh){
uint32_t size;
read(is, size);
std::insert_iterator<T> it(rh, rh.end());
for(uint32_t i=0; i<size; ++i) {
typename T::value_type x;
read(is, x);
it = x;
}
return is;
}
template <typename T, typename U>
std::ostream& write(std::ostream& os, std::map<T, U> const& rh){
return writeContainer(os, rh);
}
template <typename T, typename U>
std::istream& read(std::istream& is, std::map<T, U> rh){
return readContainer(is, rh);
}
template <typename T>
std::ostream& write(std::ostream& os, std::vector<T> const& rh){
return writeContainer(os, rh);
}
template <typename T>
std::istream& read(std::istream& is, std::vector<T>& rh){
return readContainer(is, rh);
}
int main(){
{
std::stringstream s;
std::vector<int> x = {0, 1, 2, 3};
write(s, x);
}
{
std::stringstream s;
std::map<int, int> x = {{0, 0}, {1, 2}, {2, 4}, {3, 6}};
write(s, x);
}
return 0;
}
错误:
prog.cpp: In function 'std::ostream& write(std::ostream&, const T&) [with T = unsigned int, std::ostream = std::basic_ostream<char>]':
prog.cpp:57:2: instantiated from 'std::ostream& writeContainer(std::ostream&, const T&) [with T = std::vector<int>, std::ostream = std::basic_ostream<char>]'
prog.cpp:92:30: instantiated from 'std::ostream& write(std::ostream&, const std::vector<T>&) [with T = int, std::ostream = std::basic_ostream<char>]'
prog.cpp:104:13: instantiated from here
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp: In function 'std::ostream& write(std::ostream&, const T&) [with T = int, std::ostream = std::basic_ostream<char>]':
prog.cpp:60:3: instantiated from 'std::ostream& writeContainer(std::ostream&, const T&) [with T = std::vector<int>, std::ostream = std::basic_ostream<char>]'
prog.cpp:92:30: instantiated from 'std::ostream& write(std::ostream&, const std::vector<T>&) [with T = int, std::ostream = std::basic_ostream<char>]'
prog.cpp:104:13: instantiated from here
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
因为我们没有行号,:( ,它提示第一个 write(s, x) ,在我看来似乎提示 reinterpret_cast<const char*>(const unsigned int*) 但我很确定这应该是合法的。
出了什么问题?
最佳答案
您错误地使用了 is_pod 和 is_pointer。您需要将其更改为以下内容:
static_assert(!std::is_pointer<T>::value, "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod<T>::value, "That's not a POD: can't write it");
你可以用这个测试:
write(std::cout, 1);
int *p = 0;
write(std::cout, p);
关于c++ - 转换为整数类型或其他类型不能出现在常量表达式中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7651035/
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA