Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 包含成员变量的对象的常量数组=成员变量之前索引的总和_C++_Arrays_C++11_Constexpr_Compile Time - Fatal编程技术网

C++ 包含成员变量的对象的常量数组=成员变量之前索引的总和

C++ 包含成员变量的对象的常量数组=成员变量之前索引的总和,c++,arrays,c++11,constexpr,compile-time,C++,Arrays,C++11,Constexpr,Compile Time,是否可以创建一个const对象数组,其中一个成员变量是在它之前创建的对象中的一个成员变量的总和 class Data { public: constexpr Data(uint32_t offset, uint32_t length) : m_offset(offset), m_length(length) { } uint32_t m_offset; //would like this to be calculated at compile t

是否可以创建一个const对象数组,其中一个成员变量是在它之前创建的对象中的一个成员变量的总和

class Data
{
public:
    constexpr Data(uint32_t offset, uint32_t length) :
        m_offset(offset), m_length(length)
    {
    }

    uint32_t m_offset; //would like this to be calculated at compile time
    uint32_t m_length;
};

const Data dataList[] =
{
    Data(0, 10),
    Data(10, 25),
    Data(35, 20)
};
偏移量是索引2中数组10+25=35中所有先前对象的长度之和

我希望避免手动计算偏移量


我曾经尝试过std::integral_常量和对constexpr的递归调用,但似乎没有什么能与工作解决方案相媲美,可以共享。非常感谢您的指导

如果您接受基于std::array而不是旧的C样式数组的答案,并且使用C++14而不是C++11,那么这很容易

下面是一个完整的示例

#include <array>
#include <iostream>

struct Data
{
   constexpr Data(uint32_t offset, uint32_t length) :
      m_offset(offset), m_length(length)
    { }

    uint32_t m_offset;
    uint32_t m_length;
};

template <uint32_t ... Ls>
constexpr std::array<Data, sizeof...(Ls)> getDataList ()
 {
   uint32_t  l0 { 0U };
   uint32_t  l1 { 0U };

   return { { (l0 = l1, l1 += Ls, Data(l0, l1))... } };
 }

int main ()
 {
   constexpr auto dl = getDataList<10U, 25U, 20U>();

   for ( auto const & d : dl )
      std::cout << " - " << d.m_offset << ", " << d.m_length << std::endl;
 }
现在,完整的示例变成

#include <array>
#include <iostream>

template <typename T, std::size_t N>
struct myArray
 { T arr[N]; };

struct Data
{
   constexpr Data(uint32_t offset, uint32_t length) :
      m_offset(offset), m_length(length)
    { }

    uint32_t m_offset;
    uint32_t m_length;
};

template <uint32_t ... Ls>
constexpr myArray<Data, sizeof...(Ls)> getDataList ()
 {
   uint32_t  l0 { 0 };
   uint32_t  l1 { 0 };

   return { { (l0 = l1, l1 += Ls, Data(l0, l1))... } };
 }

int main ()
 {
   constexpr auto dl = getDataList<10U, 25U, 20U>();

   for ( auto ui = 0U ; ui < 3U ; ++ui )
      std::cout << " - " << dl.arr[ui].m_offset << ", "
         << dl.arr[ui].m_length << std::endl;
 }
所以main中的循环可以使用它

// ..........................vvv
for ( auto ui = 0U ; ui < dl.dim ; ++ui )

是的,这是可能的,但在尝试使用结构之前,请先使用int进行尝试。谢谢您的快速响应!这是一个嵌入式项目,它不会编译。使用C风格的数组是否要复杂得多?我得到的错误是调用getDataList以访问未初始化的子对象Data::m_offset。它也似乎不工作在C++ 11,但确实工作在C++上14@cwhelms-对于旧的C样式数组,问题是函数无法返回它;所以我建议使用std::array:包装一个C-array并可以返回。如果问题是您不能使用std::array,那么您可以在一个简单的模板类中模拟它,该模板类封装了一个C-array;如果你愿意,我可以试着用这种方式修改我的答案;但是如果你需要一个C++11解决方案。。。好这是可能的,但我认为并不简单。我正在使用IAR,它有一些C++14支持,所以我认为我可以做一些工作。如果您可以尝试模板类包装,那就太好了。再次感谢您的帮助@cwhelm-答案改进;希望这能有所帮助。是的,这两种方法在我的PC上都能很好地使用gcc,而不是IAR编译器。我一直在调用getDataList时收到一个错误,声明:表达式必须有一个常量值。[注意]:访问未初始化的子对象成员数据::m_offset。因为这适用于大多数常见的编译器,所以我仍然将其标记为已接受。
template <typename T, std::size_t N>
struct myArray
 { static constexpr std::size_t dim { N }; T arr[dim]; };
// ..........................vvv
for ( auto ui = 0U ; ui < dl.dim ; ++ui )