我正在尝试编写一个函数,它接受各种字符串或数字(与std::to_string它们一起使用并连接它们.我只使用它来处理字符串,但是我在根据输入作为字符串或数字进行特殊处理时遇到了麻烦.
我的代码被调用如下:
stringer("hello", "world", 2, 15, 0.2014, "goodbye", "world")
这就是我所拥有的:
inline std::string stringer(const std::string &string)
{
return string;
}
template <typename T, typename... Args>
inline std::string stringer(const std::string &string, T &&val, Args &&...args)
{
return stringer(string+std::to_string(val), std::forward<Args>(args)...);
}
template <typename... Args>
inline std::string stringer(const std::string &string, Args &&...args)
{
return stringer(string, std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)
目前它正在打破任何多个字符串添加,除非以下是所有数字(由于to_string).如何根据字符串或数字进行专门化以完成上述工作?谢谢.
我正在尝试安装Gherkin 2.1.5,实际上它是https://github.com/opencongress/opencongress的依赖项.我bundle install在代码的根目录中使用安装,大约20-30个软件包已经安装,现在我坚持这个.我在跑步:
Linux 3.2.0-27-generic #43-Ubuntu SMP
Linux Mint 13 Maya
Run Code Online (Sandbox Code Playgroud)
一旦停止安装,这是输出.
---snip--(a bunch of usings)
Installing gherkin (2.1.5) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/usr/bin/ruby1.8 extconf.rb
checking for main() in -lc... yes
creating Makefile
make
gcc -I. -I. -I/usr/lib/ruby/1.8/x86_64-linux -I. -D_FORTIFY_SOURCE=2 -D_FORTIFY_SOURCE=2 -fPIC -fno-strict-aliasing -g -g -O2 -fPIC -O0 -Wall -Werror -c gherkin_lexer_ar.c
/Users/aslakhellesoy/scm/gherkin/tasks/../ragel/i18n/ar.c.rl: In function ‘CLexer_scan’:
/Users/aslakhellesoy/scm/gherkin/tasks/../ragel/i18n/ar.c.rl:198:29: error: the comparison will always evaluate as ‘true’ for the address of …Run Code Online (Sandbox Code Playgroud) 我想创建一个可以在后台运行的python脚本,但是当mouseevent或keyevent发生时打印文本.是否有任何库/内置功能来实现这一目标?或者我可以调用任何系统命令来获取此信息?成为根本不是问题.
#include <string>
#include <iostream>
std::string(foo);
int main()
{
std::cout << foo.size() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果0,而不是foo未定义的预期编译错误.
怎么能这样做?这个叫什么?
meclass.prototype.switch = function() {
var items = [];
$.getJSON('http://localhost/jsoner.php', function(data) {
$.each(data, function(key, val) {
items.push(val);
alert(items[0]); //this works
});
});
alert(items[0]); //this does not
}
Run Code Online (Sandbox Code Playgroud)
我一直在修补这个问题而没有真正得到它.我在我的所有jquery函数中都遇到了这个问题所以它是基本的,我只是没有学习 - 而且没有运气找到答案.
我有一个处理任意大网格的函数。我需要计算由于使用了另一个数字的幂的网格是否适合双精度std::pow。如果不能,我想采用不同的分支并使用 gnu 多精度库而不是普通库。
有没有一种快速的方法来查看是否:
int a = 1024;
int b = 0-10;
if(checkPowFitsDouble(a, b)) {
long c = static_cast<long>(std::pow(a, b)); //this will only work if b < 6
} else {
mpz_t c; //yada yada gmp
}
Run Code Online (Sandbox Code Playgroud)
我完全被 checkPowFitsDouble 难住了;也许有一些我不知道的数学技巧。
现在我有这个代码:
uint64_t buffer = 0;
const uint8_t * data = reinterpret_cast<uint8_t*>(&buffer);
Run Code Online (Sandbox Code Playgroud)
这样做有效,但由于悬挂指针(看起来也很难看)似乎有风险.我不想坐在那里的裸指针.我想做这样的事情:
uint64_t buffer = 0;
const std::array<uint8_t, 8> data = partition_me_softly(buffer);
Run Code Online (Sandbox Code Playgroud)
是否有和C++ 11型结构,它允许我进入一个安全的容器这一点,最好一个std::array出来的unsigned int是这样不诱导开销?
如果没有,那么改进此代码以获得更安全的理想方法是什么?
所以我修改了dauphic的答案,使其更具通用性:
template <typename T, typename U>
std::array<T, sizeof(U) / sizeof(T)> ScalarToByteArray(const U v)
{
static_assert(std::is_integral<T>::value && std::is_integral<U>::value,
"Template parameter must be a scalar type");
std::array<T, sizeof(U) / sizeof(T)> ret;
std::copy((T*)&v, ((T*)&v) + sizeof(U), ret.begin());
return ret;
}
Run Code Online (Sandbox Code Playgroud)
这样你可以使用更多类型,如:
uint64_t buffer = 0;
ScalarToByteArray<uint8_t>(buffer);
Run Code Online (Sandbox Code Playgroud) 我正在为freeimage编写一个小包装器,用于图像加载和像素抓取等.我有一个PImage处理所有加载和显示的类,在它里面它有一个PixelColorBuffer类.我PixelColorBuffer使用方便的方法从中获取unsigned chars texturebuffer并将它们转换为另一个被调用的类color(我排除它因为它工作正常).我也希望能够使用这个PixelColorBuffer类设置像素,这就是为什么它有colortobuffer和buffertocolor.我实例化PixelColorBuffer了一个指向它的位置的指针unsigned char array(注意:它包含图片的rgba值).但是,这似乎有效,但当我调用get(10, 10)加载并显示的图像时,我得到以下内容:
(GNU Debugger)
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bc66d9 in cprocessing::PixelColorBuffer::buffertocolor (this=<optimized out>, n=<error reading variable: Unhandled dwarf expression opcode 0x0>) at pixelcolorbuffer.cpp:17
17 c.rgba[0]=(*b)[(n*4)+0];
Run Code Online (Sandbox Code Playgroud)
在PImage和PixelColorBuffer类编译成.so和链接正确.我假设我在设置指针时做错了,这是我第一次处理指针的指针......但我不能为我的生活弄清楚我做错了什么.这是所有相关的代码.
///MAIN_PROGRAM.CPP
PImage t;
t.loadImage("image.png"); //loads image (works)
image(t, mouseX, mouseY); //draws image (works)
color c = t.get(10, 10); //SEGFAULT …Run Code Online (Sandbox Code Playgroud)