微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

decltype 可以减少重新编译吗

如何解决decltype 可以减少重新编译吗

我正在做一个cpp项目,假设有这么一段代码

// header1.h
struct Test {
    int a;
    bool func(int b) { return a < b; }
};

// header2.h
#include "header1.h"
void myfunc(int aa) {
    Test t;
    t.a = aa;
    bool res = t.func(3);
    // something else
}

// header3.h
#include "header1.h"
void myfunc(decltype(Test::a) aa) {   // CHANGED HERE
    Test t;
    t.a = aa;
    bool res = t.func(3);
    // something else
}

我总是像 headedr2.h 那样编码。但是今天,我遇到了这样一个案例,Test::a中的header1.h类型以后可能会变成uint8_tint32_t等。如果它改变了,header2.h 也应该改变。 (我不想进行任何隐式转换。)

也就是说,如果 header1.h 发生变化,我必须更改 header2.h,因此,包括 header2.h 在内的所有文件都必须重新编译。

现在,我在考虑是否可以像使用 decltype 一样使用 header3.h 来避免重新编译。换句话说,我问我是否编码 header3.h 而不是 header2.h,是否可以避免在更改 header3.h 的类型后重新编译包含 Test::a文件header1.h

解决方法

不,如果更改 header1.h,则必须重新编译包含 header1.h 的所有内容。

原因是:编译器如何在不知道 myfunc 的参数类型或不知道您在 Test 中使用的 myfunc 类型的情况下生成它的代码?

>

如果您更改 struct Test,更改其成员的类型,则使用过时的 Test 声明生成的所有代码都可能无法在链接阶段编译,或者可能有运行时不可预测的行为。

,

从编译开始,所有包含 "header1" 的文件都应该重新编译。

使用 decltype(Test::a) 需要 #include 而直接使用 int 不需要(定义无论如何都需要它,但可以在 cpp 中)。

使用 decltype(Test::a) 可以避免在多个地方更新代码。

decltype(Test::a) 设置别名可能是有意义的,例如

struct Test {
    using type_a = int;
    type_a a;
    bool func(type_a b) const { return a < b; }
};

并使用看起来更惯用的 Test::type_a

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。