小编Vic*_*ian的帖子

为什么 unique_ptr 需要构造函数中的完整类型?

来自std::unique_ptr 需要知道 T 的完整定义吗?,我知道如果一个类A有一个成员unique_ptr<T>,那么T在析构函数中应该是一个完整的类型~A()。但是,我遇到了一种情况,构造函数A()也需要完整的类型T,请参见下面的代码:

\n
// a.h -------------------------------\n#pragma once\n#include <memory>\nstruct B;\nstruct A {\n  A(); // <---\n  ~A();\n  std::unique_ptr<B> ptr;\n};\n\n// a.cpp -------------------------------\n#include "a.h"\nstruct B {};\nA::A() = default; // <---\nA::~A() = default;\n\n// main.cpp -------------------------------\n#include "a.h"\nint main() {A a;}\n
Run Code Online (Sandbox Code Playgroud)\n

如果构造函数的定义A::A()被移动到头文件中a.h,编译器将会报错error: invalid application of \xe2\x80\x98sizeof\xe2\x80\x99 to incomplete type \xe2\x80\x98B\xe2\x80\x99。为什么会发生这种情况?有这方面的参考资料吗?

\n

顺便说一句,我在 Ubuntu 18.04 上使用 gcc-7.5.0,启用了 c++17。

\n
\n

在评论中编辑@463035818_is_not_a_number。完整的错误信息是:

\n
[1/2] Building CXX …
Run Code Online (Sandbox Code Playgroud)

c++ stl unique-ptr incomplete-type c++11

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

CRTP:将类型从派生类传递到基类

CRTP 中,基类可以使用派生类的函数和变量。但是,派生类的类型不能直接被基类使用,见下面的代码:

#include <iostream>

template <class Derived>
class A {
public:
    //using Scalar = typename Derived::Scalar; // Error!
    static constexpr int NA1 = Derived::NB1;
    static constexpr int NA2 = Derived::NB2;
    static constexpr int NA3 = Derived::NB3;
};

template <int _N = 2>
class B : public A<B<_N>> {
public:
    using Scalar = double;
    static constexpr int NB1 = 1;
    static constexpr int NB2 = _N;
    static constexpr int NB3 { sizeof(Scalar) };
};

int main(int argc, char** argv) …
Run Code Online (Sandbox Code Playgroud)

c++ templates crtp

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

初始化类模板的静态 constexpr 成员变量

情况如下:Foo带有模板参数的类int N有一个静态成员变量float val。的值val对应于N并且永远不会改变,所以我希望它是constexpr

我知道初始化静态 constexpr 成员变量的常用方法是:

// ok, but not what I want
template <int N>
struct Foo {
    static constexpr float val { 0.0f };
};
template <int N>
constexpr float Foo<N>::val;
Run Code Online (Sandbox Code Playgroud)

但是因为val类范围内初始化的,所以我不能val为不同的N.

如果val不是constexpr但是const,这有效:

// ok, but not what I want
template <int N>
struct Foo {
    static const float val;
};
template …
Run Code Online (Sandbox Code Playgroud)

c++ templates constexpr c++14 c++17

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

boost::pool_allocator 明显慢于 std::allocator

我正在学习内存池,并尝试boost::pool_allocator在我的项目中使用。根据文档,我做了一个关于时间成本的小测试:

template <typename Alloc>
void test()
{
    using namespace std::chrono;
    auto t0 = high_resolution_clock::now();
    for (int i = 0; i < 1000; ++i) {
        std::vector<int, Alloc> vec;
        for (int j = 0; j < 10000; ++j)
            vec.push_back(i + j);
    }
    auto t1 = high_resolution_clock::now();
    auto time_ms = duration<double>(t1 - t0).count() * 1e3;
    cout << "time cost: " << time_ms << " ms" << endl;
}

int main()
{
    test<std::allocator<int>>();
    test<boost::pool_allocator<int>>();
}
Run Code Online (Sandbox Code Playgroud)

结果是:

time cost: 3.97602 ms …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-pool

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