c 11类成员数组大小constexpr前向声明

我想在使用它们后从我的include链中排除一些标题.据我所知,c 11中没有排除“header.h”.

伪代码一厢情愿:

#include "the_bad_header.h" //long includechain with later unused declarations
class bulky { ... };
constexpr std::size_t bulkysize = sizeof(bulky);
forget everything included and class bulky and remember only bulkysize

问题变得明显的例子如下.请不要争辩这不是一个严重的问题.该示例被细分以显示最小的抽象语言用法.我将描述老式的解决方案及其缺点.

旧式解决方案

justanotherheader.h:

class bulkywrap
{
public:
    bulkywrap();
protected:
    friend class bulkywrap_pImpl;
    bulkywrap_pImpl *const pImpl; //opaque pointer, private implementation
};

justanothercppunit.cpp:

#include "justanotherheader.h"
#include "boost/lotsofheaders.hpp"
//#include more and more headers of highly complex libraries so adding millions of known types and other identifiers, macros, and so on

class bulkywrap_pImpl
{
    //lots of members of types used from the other libraries
};

bulkywrap::bulkywrap()
: pImpl( new bulkywrap_pImpl() )
{}

我目前的解决方案

justanotherheader.h:

#include "stdint.h" // this is the only header I like to use, but also unnecessary.
#define UNKNOWNSIZE 12345
class bulkywrap
{
public:
    bulkywrap();
protected:
    friend class bulkywrap_pImpl;
    bulkywrap_pImpl *const pImpl; //opaque pointer, private implementation
    uint8_t pImpl_Placement[UNKNOWNSIZE]; //placement new for pImpl
};

justanothercppunit.cpp:

#include "justanotherheader.h"
#include "boost/lotsofheaders.hpp"
//#include more and more headers of highly complex libraries so adding millions of known types and other identifiers, macros, and so on

class bulkywrap_pImpl
{
    //lots of members of types used from the other libraries
};

bulkywrap::bulkywrap()
: pImpl( new(this->pImpl_Placement) bulkywrap_pImpl() ) //using this here is safe
{}

所以,上面的代码是有效的.优点是:隐藏复杂性并且没有运行时动态内存间接.咦?我的意思是,placement new允许整个对象放在堆栈上,所有成员地址在编译时都是已知的.我的尝试是在使用不透明指针的界面设计时获得最佳性能.

如果你认为:“这种性能优势不值得思考.”那么请留下这个问题.

我期望的解决方案

justanotherheader.h:

#include "stdint.h" // this is the only header I like to use, but also unnecessary.

constexpr std::size_t get_bulkywrap_pImpl_Size( void ); //constexpr function forward declaration
extern constexpr std::size_t bulkywrap_pImpl_Size; //constexpr literal forward declaration with external initialization

class bulkywrap
{
public:
    bulkywrap();
protected:
    friend class bulkywrap_pImpl;
    bulkywrap_pImpl *const pImpl; //opaque pointer, private implementation
    uint8_t pImpl_Placement[get_bulkywrap_pImpl_Size()]; //undefined constexpr used
    uint8_t pImpl_Placement[bulkywrap_pImpl_Size]; //alternative to above. undefined constexpr used
};

justanothercppunit.cpp:

#include "justanotherheader.h"
#include "boost/lotsofheaders.hpp"
//#include more and more headers of highly complex libraries so adding millions of known types and other identifiers, macros, and so on

class bulkywrap_pImpl
{
    //lots of members of types used from the other libraries
};

constexpr std::size_t get_bulkywrap_pImpl_Size( void )
{
    return sizeof(bulkywrap_pImpl);
}
constexpr std::size_t bulkywrap_pImpl_Size = sizeof(bulkywrap_pImpl);

bulkywrap::bulkywrap()
: pImpl( new(this->pImpl_Placement) bulkywrap_pImpl() ) //using this here is safe
{}

在我目前的解决方案中,我需要验证sizeof(bulkywrap_pImpl)并手动调整UNKNOWNSIZE.
我认为目前无法从汇编单元获取任何信息给其他人.我知道这通常是有充分理由的,但这限制了c 11的可能性.

我想指出:

jtc1 sc22 wg21 paper n3337

jtc1 sc22 wg21 paper n3308

请帮我查找天气信息以及标准不允许的原因.

但此外,我想找到一个解决方案,如何在编译期间将一些文字常量从编译单元导出到另一个编译单元.它只是一个文字,因此所有语句和表达都不受其影响.因此编译不依赖于数组大小的来源.

我的建议导致了ISO-jtc1-sc22-wg21和编译器开发人员的一些工作,但我没有看到模板和constexpr之间有任何相关的区别,因为每个定义必须出现在同一个翻译单元中.这使得模块化编程和干净的接口变得虚假.

不,我不想使用预处理器宏,动态新的或虚拟的成员函数.重要的是最大的const-correctness,因为类的大小是const.

请帮忙

最佳答案 你不能同时拥有“编译时”和“来自另一个编译单元”.还不清楚为什么你需要忘记成功解析的标题.解析已消耗的时间.我建议你创建另一个大小常量的标题,从两个文件中包含它,并将static_assert添加到pimpl文件,检查该常量> = sizeof(pimpl).您可以通过编译源文件(包括pimpl和执行cout<< sizeof(pimpl))来生成此标头作为构建系统的一部分.另外我建议你不要浪费时间和空间用于pimpl指针并用成员函数替换它,返回正确转换缓冲区的地址.你也没能显示你在哪里调用pimpl的析构函数.同时实现分配/复制/移动/交换将会很有趣

点赞