在Modern Effective C++中,"Iterm 19:std::shared_ptr用于共享所有权资源管理.",第133-134页,它说:
std :: shared_ptr支持对单个对象有意义的派生到基指针转换,但是当应用于数组时,它会在类型系统中打开.(因此,std :: unique_ptr API禁止此类转换.)
"类型系统中的开孔"是什么意思?
为什么std::unique_ptr<T[]>API会禁止派生到基指针转换?
它怎么能禁止转换呢?
以下代码中的4个类:A,B,C和D.
他们都有一个成员operator new[].
除了,
operator delete[].输出size成员的参数operator new[]和sizeof4个类的参数:
new[] A 40
new[] B 40
new[] C 48
new[] D 48
sizeof(A) 4
sizeof(B) 4
sizeof(C) 4
sizeof(D) 4
Run Code Online (Sandbox Code Playgroud)
差异的原因是size什么?
代码(丑陋我知道):
#include <iostream>
using namespace std;
class A {
int i;
public:
static void* operator new[](std::size_t size) throw(std::bad_alloc) {
cout << "new[] A " << size << endl;
return malloc(size);
}
};
class B {
int i; …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
Mat a = (Mat_<double>(3, 3) << 0, 1, 2, 3, 4, 5, 6, 7, 8);
cout << a << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何使用C++实现OpenCV Mat的逗号分隔初始化程序?
在"0"之后"1"如何进入垫子?
例如,我有以下汇编代码objdump。的地址f()是080001d4。但printf("%x", f)输出080001d5. 并且f()可以通过(*((int (*)())080001d5))()但是完成(*((int (*)())080001d4))()。
为什么函数地址有一个字节的偏移量?
080001d4 <f>:
80001d4: 2000 movs r0, #0
80001d6: 4770 bx lr
Run Code Online (Sandbox Code Playgroud) #include <vector>
using namespace std;
struct TempData {
vector<int> data;
TempData() {
for(int i = 0; i < 100; i++) data.push_back(i);
}
// vector<int> GetData() { // function 1
// return move(data);
// }
vector<int>&& GetData() { // function 2
return move(data);
}
};
int main() {
vector<int> v;
{
TempData td;
v = td.GetData();
}
}
Run Code Online (Sandbox Code Playgroud)
function 1和 和有function 2什么区别?
将function 1构造一个 temp vector,move(data)然后将 temp 分配vector给v?
没有更多细节可以添加...
#include <memory>
void f1(std::shared_ptr<bool> ptr) {}
int main() {
f1(0); // OK
f1(1); // compilation error: could not convert ‘1’ from ‘int’ to ‘std::shared_ptr<bool>’
}
Run Code Online (Sandbox Code Playgroud)
既为int,为什么0但1可以转换为std::shared_ptr<T>?
如何从转换的残疾1来std::shared_ptr<T>编译时进行检查?
如何从转换的残疾1来std::nullptr_t编译时进行检查?
#include <iostream>
using namespace std;
struct A {
A(int i) {
cout << "1 args" << endl;
}
A(int i, int j) {
cout << "2 args" << endl;
}
};
void foo(int i) {
cout << "1 args" << endl;
}
void foo(int i, int j) {
cout << "2 args" << endl;
}
int main() {
int i, j;
A(i, j);
(A)(i, j);
foo(i, j);
(foo)(i, j);
}
Run Code Online (Sandbox Code Playgroud)
输出:
2 args
1 args
2 args
2 args
Run Code Online (Sandbox Code Playgroud)
我知道结果"1 …
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A()" << endl;
}
A(const A& a) {
cout << "A(const A& a)" << endl;
}
A(A&& a) {
cout << "A(A&& a)" << endl;
}
A& operator=(const A& a) {
cout << "operator=(const A& a)" << endl;
return *this;
}
A& operator=(A&& a) {
cout << "operator=(A&& a)" << endl;
return *this;
}
~A() {
cout << "~A()" << endl;
};
};
A foo() {
A …Run Code Online (Sandbox Code Playgroud) 码:
#include <cstdio>
int main() {
unsigned char a = -300.f;
printf("%d\n", a);
}
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会编制:
g++ test.cpp -o test -std=c++11
test.cpp: In function ‘int main()’:
test.cpp:4:21: warning: overflow in implicit constant conversion [-Woverflow]
unsigned char a = -300.f;
^
Run Code Online (Sandbox Code Playgroud)
GCC结果:
0
Run Code Online (Sandbox Code Playgroud)
GCC版本:
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
Run Code Online (Sandbox Code Playgroud)
Clang编译:
clang++ test.cpp -o test -std=c++11
test.cpp:4:21: warning: implicit conversion from 'float' to 'unsigned char' changes value from 300 to 255
[-Wliteral-conversion]
unsigned char a = -300.f;
~ ^~~~~
1 warning generated.
Run Code Online (Sandbox Code Playgroud)
铿锵的结果: …
有没有办法在没有基类名称和范围解析运算符的情况下引用基类模板的成员变量?
template<typename D>
struct B0 {
int value;
};
struct D0: B0<D0> {
D0() {
B0<D0>::value = 1; // OK.
value = 1; // OK without `B0<D0>::`.
}
};
template<typename T>
struct B1 {
T value;
};
template<typename T>
struct D1: B1<T> {
D1() {
B1<T>::value = 1; // OK.
// value = 1; // Compile error without `B1<T>::`.
// Compile error: use of undeclared identifier 'value'
// `B1<T>::` is tedious everywhere `value` is referenced.
}
};
template<typename T, typename …Run Code Online (Sandbox Code Playgroud) #version 330和之间有什么区别#version 330 core?
请问core怎么回事?
是否存在这种编译时计算可以使编译器陷入无限循环?
无限循环不会消耗不断增加的内存吗?或者它可能会因缺乏内存而停止。
#include <immintrin.h>
constexpr int n_batch = 10240;
constexpr int n = n_batch * 8;
#pragma pack(32)
float a[n];
float b[n];
float c[n];
#pragma pack()
int main() {
for(int i = 0; i < n; ++i)
c[i] = a[i] * b[i];
for(int i = 0; i < n; i += 4) {
__m128 av = _mm_load_ps(a + i);
__m128 bv = _mm_load_ps(b + i);
__m128 cv = _mm_mul_ps(av, bv);
_mm_store_ps(c + i, cv);
}
for(int i = 0; i < …Run Code Online (Sandbox Code Playgroud) c++ ×11
c++11 ×2
constructor ×2
destructor ×2
shared-ptr ×2
arm ×1
assembly ×1
avx ×1
base-class ×1
clang ×1
comma ×1
compile-time ×1
copy-elision ×1
g++ ×1
gcc ×1
glsl ×1
mat ×1
move ×1
name-lookup ×1
new-operator ×1
nullptr ×1
opencv ×1
opencv-mat ×1
opengl ×1
pointers ×1
simd ×1
templates ×1
thumb ×1
unique-ptr ×1
xvalue ×1