我找到了这样的代码:
template <typename T, typename T1> auto compose(T a, T1 b) -> decltype(a + b) {
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
我想到了所有细节,这对我来说是新的,但只有一个.请告诉我,在哪里可以阅读,箭头操作符(->)在函数标题中的含义是什么?我纯粹从逻辑上说,那个->算子确定了一个类型,auto但是我希望得到这个,但找不到信息.
我正在使用C++(而不是C++ 11).我需要在类中创建一个指向函数的指针.我尝试做以下事情:
void MyClass::buttonClickedEvent( int buttonId ) {
// I need to have an access to all members of MyClass's class
}
void MyClass::setEvent() {
void ( *func ) ( int );
func = buttonClickedEvent; // <-- Reference to non static member function must be called
}
setEvent();
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:"必须调用非静态成员函数的引用".如何制作指向MyClass成员的指针?
在C中,您可以使用strdup简洁地分配缓冲区并将字符串复制到其中.然而,据我所知,一般记忆没有类似的功能.例如,我不能说
struct myStruct *foo = malloc(sizeof(struct myStruct));
fill_myStruct(foo);
struct myStruct *bar = memdup(foo, sizeof(struct myStruct));
// bar is now a reference to a new, appropriately sized block of memory,
// the contents of which are the same as the contents of foo
Run Code Online (Sandbox Code Playgroud)
那么,我的问题有三个:
malloc和memcpy?strdup但不包括memdup?我试图用一些整数初始化一个2d数组.如果我将数组初始化为0我得到正确的结果但是如果我使用其他整数我得到一些随机值.
int main()
{
int array[4][4];
memset(array,1,sizeof(int)*16);
printf("%d",array[1][2]); <---- Not set to 1
}
Run Code Online (Sandbox Code Playgroud) 我发现这个程序的编译器之间存在一些不一致,
struct A {
};
struct B : public A {
float m;
};
struct C : public A {
B b;
float n;
};
struct D : public A {
float n;
B b;
};
static_assert(sizeof(A) == 1, "");
static_assert(sizeof(B) == 4, "");
static_assert(sizeof(C) == 8, ""); // most compilers say this is 12
static_assert(sizeof(D) == 8, "");
Run Code Online (Sandbox Code Playgroud)
大多数编译器断言sizeof(C)== 8说sizeof(C)实际上是12.我发现的唯一编译器没有,并说它是8是Microsoft Visual Studio 2010.
我被告知的原因是,比我更聪明的人,是B中有两个单独的A参考,需要保留彼此不同的个体偏移.首先,从C导出的A在偏移0处,并且第二个A内部成员b不能与第一个A在0处的偏移量相同,因此插入了4个字节的填充.
由于大多数编译器已经实现了这种行为,我想知道你需要什么案例来确保两个A都有不同的引用?寻找一些关于为什么会这样的直觉?
有人说这可能是标准要求的条件,我们很好奇它的原因是什么?
谢谢
我发现自己在打字
double foo=1.0/sqrt(...);
Run Code Online (Sandbox Code Playgroud)
很多,我听说现代处理器有内置的反平方根操作码.
是否存在C或C++标准库的反平方根函数
1.0/sqrt(...)吗?1.0/sqrt(...)?如果我在Windows和Linux(ubuntu)上编译以下c行,我会得到不同的结果.我想避免.我该怎么做?
double a = DBL_EPSILON;
double b = sqrt(a);
printf("eps = %.20e\tsqrt(eps) = %.20e\n", a, b);
Run Code Online (Sandbox Code Playgroud)
linux输出:
eps = 2.22044604925031308085e-16 sqrt(eps) = 1.49011611938476562500e-08
Run Code Online (Sandbox Code Playgroud)
窗口输出:
eps = 2.22044604925031310000e-016 sqrt(eps) = 1.49011611938476560000e-008
Run Code Online (Sandbox Code Playgroud)
在linux上测试用gcc和clang在32位和64位系统上的结果相同.在使用32位的gcc-mingw和32位和64位的visual-studio测试的Windows上,也有相同的结果.
当抛出异常C++并且堆栈被解开时,如何选择正确的处理程序(catch子句)来处理异常?
void f1()
{
throw 1;
}
void f2()
{
try
{
f1();
}
catch(const char* e)
{
std::cout << "exc1";
}
}
...
try
{
f2();
}
catch(int& e)
{
std::cout << "exc2";
}
...
Run Code Online (Sandbox Code Playgroud)
例如,这个代码不出所料地打印,"exc2"因为catch(int& e)它能够处理1 int类型化的对象.
我不明白的是,这怎么可以静态解决?还是动态解决?是否传播了类型信息?
我有以下代码:
int main(int argc, char** argv)
{
char* p = new char[11];
strcpy(p, "1234567890");
cout << strlen(p) << endl;
delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它分配11个字节,然后复制一个10字节的字符串加上一个nul终结符.这对我来说似乎很正确.
但如果我用Valgrind运行它,我得到这个:
bash-4.3$ valgrind ./a.out
...
==44295== Command: ./a.out
==44295==
==44295== Invalid read of size 8
==44295== at 0x3E6073382F: __strlen_sse42 (in /lib64/libc-2.12.so)
==44295== by 0x4008A9: main (in /bb/mbig_new2/mbig3978/bbgithub/tsacqdata/tsacqdata/unit_test/Cache/a.out)
==44295== Address 0x4c2d048 is 8 bytes inside a block of size 11 alloc'd
==44295== at 0x4A06FE8: operator new[](unsigned long) (vg_replace_malloc.c:363)
==44295== by 0x40087E: main (in /bb/mbig_new2/mbig3978/bbgithub/tsacqdata/tsacqdata/unit_test/Cache/a.out)
...
为什么Valgrind不喜欢那个 …
我试图想出一种方法将2个向量和一个整数合并为一个向量.即
return data.push_back(fn(data1), mid, fn(data2));
Run Code Online (Sandbox Code Playgroud)
NB这是一个递归函数.向量数据在到达return语句之前具有存储在其中的值.我需要使用return语句中的值更新数据中的值.
我完全不知道如何去做这件事.我一直在寻找几个小时,但似乎什么都没有用!
非常感谢任何指导.