小编cha*_*ink的帖子

std :: unique_ptr <T []> API禁止派生到基指针转换

Modern Effective C++中,"Iterm 19:std::shared_ptr用于共享所有权资源管理.",第133-134页,它说:

std :: shared_ptr支持对单个对象有意义的派生到基指针转换,但是当应用于数组时,它会在类型系统中打开.(因此,std :: unique_ptr API禁止此类转换.)

"类型系统中的开孔"是什么意思?

为什么std::unique_ptr<T[]>API会禁止派生到基指针转换?

它怎么能禁止转换呢?

c++ shared-ptr unique-ptr c++11

14
推荐指数
1
解决办法
363
查看次数

如果类具有析构函数/ delete [],则成员运算符new []的参数"size"会增加

以下代码中的4个类:A,B,C和D.

他们都有一个成员operator new[].

除了,

  • B有一个构造函数;
  • C有一个析构函数;
  • D有一名成员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)

c++ destructor new-operator delete-operator

8
推荐指数
1
解决办法
462
查看次数

如何使用C++实现OpenCV Mat的逗号分隔初始化程序?

#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"如何进入垫子?

c++ opencv comma mat opencv-mat

4
推荐指数
1
解决办法
633
查看次数

ARM汇编中的函数地址有一个字节的偏移量?

例如,我有以下汇编代码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)

assembly arm thumb

3
推荐指数
1
解决办法
2331
查看次数

成员函数返回成员变量的右值引用

#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 vectormove(data)然后将 temp 分配vectorv

没有更多细节可以添加...

c++ move rvalue-reference move-semantics xvalue

3
推荐指数
1
解决办法
481
查看次数

为什么我可以将0转换为std :: shared_ptr <T>而不是1?

#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,为什么01可以转换为std::shared_ptr<T>

如何从转换的残疾1std::shared_ptr<T>编译时进行检查?

如何从转换的残疾1std::nullptr_t编译时进行检查?

c++ pointers shared-ptr nullptr c++11

3
推荐指数
2
解决办法
189
查看次数

C++中构造函数可能存在歧义

#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 …

c++ constructor

2
推荐指数
1
解决办法
47
查看次数

C++中局部变量和返回变量的构造和破坏

#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)

c++ constructor destructor move-constructor copy-elision

2
推荐指数
1
解决办法
82
查看次数

GCC与Clang之间的不同行为

码:

#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)

铿锵的结果: …

c++ gcc g++ clang

2
推荐指数
1
解决办法
80
查看次数

引用基类模板成员变量的简单方法

有没有办法在没有基类名称和范围解析运算符的情况下引用基类模板的成员变量?

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)

c++ templates base-class class-template name-lookup

2
推荐指数
1
解决办法
117
查看次数

#version 330和#version 330核心有什么区别?

#version 330和之间有什么区别#version 330 core

请问core怎么回事?

opengl glsl

0
推荐指数
1
解决办法
3209
查看次数

编译时计算的存在会使编译器陷入无限循环

是否存在这种编译时计算可以使编译器陷入无限循环?

无限循环不会消耗不断增加的内存吗?或者它可能会因缺乏内存而停止。

c++ metaprogramming compile-time template-meta-programming

0
推荐指数
1
解决办法
41
查看次数

_mm256_load_ps 在调试模式下导致 google/benchmark 出现分段错误

  • 以下代码可以在发布和调试模式下运行。
#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++ simd segmentation-fault avx google-benchmark

0
推荐指数
1
解决办法
298
查看次数