简体   繁体   English

C ++:如何从标准布局类模板中优化出空数据成员?

[英]C++: How do I optimize out an empty data member from a standard layout class template?

Can I have a standard layout instance of a class template with an optional non-static data member? 我是否可以使用可选的非静态数据成员的类模板的标准布局实例? Being "optional" means that the data member in question must not be present in certain instances of the template class. “可选”意味着所讨论的数据成员不得出现在模板类的某些实例中。

That is, given: 那是,给定:

template <typename T>
struct Dependent
{
    T      m_defaultValue;
};

template <>
struct Dependent<double>
{
};

template <typename T>
struct MyData
{
    T*              m_data;
    Dependent<T>    m_optional;

    T LotsOfAccessorMethods() const;
};

I want the layout of MyData<int> to be equivalent to struct { int* x; int y; } 我希望MyData<int>的布局等同于struct { int* x; int y; } struct { int* x; int y; } struct { int* x; int y; } . struct { int* x; int y; }

And I want the layout of MyData<double> to be equivalent to struct { double* x; } 我希望MyData<double>的布局等同于struct { double* x; } struct { double* x; } . struct { double* x; }

The problem here is that the solution must comply with the following key restrictions: 这里的问题是该解决方案必须符合以下主要限制:

  1. MyData<> must meet the requirements for standard layout class . MyData <>必须满足标准布局类的要求 Which means that all data members should be located in one class. 这意味着所有数据成员应位于一个类中。 It is impossible to refactor optional data member(s) to a base class. 将可选数据成员重构为基类是不可能的。

  2. MyData<> must not be specialized , not even partially. MyData <>必须不是专业的 ,甚至不是部分的。 In real task it has quite some methods and fields, and repeating them in all specializations would ruin the whole point of having a generic template. 在实际任务中它有相当多的方法和领域,并且在所有专业化中重复它们会破坏拥有通用模板的全部意义。


Background: 背景:

I wish to communicate a lot of special container structures to a DLL/SO which is out of my control. 我希望将很多特殊容器结构传达给DLL / SO,这是我无法控制的。 The latter means that I should assume that DLL could have been written is some other language, or perhaps just built with another compiler. 后者意味着我应该假设DLL可能是用其他语言编写的,或者可能只是用另一个编译器构建的。 This looks like a job for Standard Layout structures. 这看起来像标准布局结构的工作。

Those container structures have very similar set of data members and member functions, so it would be a shame (and a maintenance nightmare) not to merge them all to a class template. 这些容器结构具有非常相似的数据成员和成员函数集,因此将它们全部合并到类模板将是一种耻辱(以及维护噩梦)。

However, some of those containers must have extra data members (tag ids, special values, etc). 但是, 其中一些容器必须具有额外的数据成员(标记ID,特殊值等)。 Hence the question. 因此问题。

How about this: 这个怎么样:

#include <type_traits>
template <typename T>
struct MyData {
    struct general_impl { T* m_data; T optional; };
    struct double_impl  { T* m_data; };

    using data_type = typename std::conditional<
        std::is_same<T, double>::value,
        double_impl,
        general_impl>::type;

    data_type data;

    T LotsOfAccessorMethods() const;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中访问专用模板类中的模板类成员字段? - How do I access a template class member field in a specialised template class in C++? 在C ++中,如何为一个类创建模板以使用另一个类和该类的成员? - In C++, how do I template a class to use another class and a member of that class? C ++标准中“类的成员模板”指的是什么? - What does “a member template of a class” refer to in the C++ standard? 如何向前声明模板类并将其用作C ++中的成员数据? - How to forward declare a template class and use it as a member data in C++? C ++如何将一个模板类转换为另一个模板类? - C++ How do I cast from 1 template class to another? 现代 C++ 编译器是否优化了对类的同一数据成员的重复访问? - Do modern C++ compilers optimize away repeated accesses to the same data member of a class? 如果不使用模板参数,如何将c ++类模板成员函数放在cpp文件中? - How do I put c++ class template member functions in a cpp file if they don't use template arguments? 如何从模板 class A 访问在 c++ 的模板 B class 中声明为私有字段的结构? - How do I access from template class A a struct declared as private field in template B class in c++? C++ class 与模板成员变量。 和参数 memory 输出 - C++ class with template member variable. and parameter memory out 如何从C函数访问C++类数据成员 - how to access C++ class data member from C function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM