用联合初始化C ++结构的正确方法是什么?

问题描述

MISRA-C ++规则8-5-2要求使用正确的花括号初始化C ++结构。 我有一个带有联合的结构,但我找不到能满足此规则的大括号的正确组合。我不确定我的代码是否正确或来自静态代码分析器工具的误报。

这是结构:

typedef struct XMC_VADC_RESULT_CONfig
{
    union
    {
       struct
       {
          uint32_t                         : 16;
          uint32_t data_reduction_control  : 4;  /**< Configures the data reduction stages */
          uint32_t post_processing_mode    : 2;  /**< Result data processing mode. Uses @ref  XMC_VADC_DMM_t
                                                      For normal operation select
                                                      XMC_VADC_DMM_t::XMC_VADC_DMM_REDUCTION_MODE
                                                      and data_reduction_control as 0*/
          uint32_t                         : 2;
          uint32_t wait_for_read_mode      : 1;  /**< Allow the conversion only after prevIoUs results are read*/
          uint32_t part_of_fifo            : 2;  /**< Make the result register a part of Result FIFO? */
          uint32_t                         : 4;
          uint32_t event_gen_enable        : 1;  /**< Generates an event on availability of new result. */
       };
       uint32_t g_rcr;
    };
} XMC_VADC_RESULT_CONfig_t;

这是我的初始化代码

    const XMC_VADC_RESULT_CONfig_t resultConfig = 
{
  {
    {
    .data_reduction_control     = 0U,// No Accumulation
    .post_processing_mode       = static_cast<uint32_t>(XMC_VADC_DMM_REDUCTION_MODE),.wait_for_read_mode         = 0U,// disabled
    .part_of_fifo               = 0U,// No FIFO
    .event_gen_enable           = 0U                                                    // disable Result event
    }
  }
};

我也尝试过删除一组牙套,但这没有帮助。 大括号的正确数量是多少?

解决方法

  • 正确的,符合要求的大括号放置应为const XMC_VADC_RESULT_CONFIG_t resultConfig = { 0u };
  • MISRA-C ++需要C ++ 03,所以您不能使用其他任何东西。
  • 指定的初始值设定项(.name语法)是C事物,仅在C99和更高版本中存在。 MISRA-C:2004中不允许使用它们,但MISRA-C:2012中(其中有一些特殊规则)不允许使用它们。在C ++中,它们只是在最近才引入,不允许在MISRA兼容应用程序中使用C ++版本。
  • 通常,在任何MISRA中都不允许使用
  • union类型的修剪,特别是在C ++中,它是未定义的行为,尤其是不允许。 C中存在该规则的一些例外,但C ++中没有。
  • 在任何关键应用程序中(无论是否存在MISRA),位域的存在都是令人质疑的做法,因为它们的标准化程度很低。

摘要:您不能在任何形式的MISRA应用程序中使用此代码。删除并集和位字段,并用按位运算符和位掩码替换它们。

,

C ++没有designated initializers until C++20,因此您将其删除。


Ext.onReady(() => {
  Ext.create({
    xtype: 'cartesian',renderTo: element,// rendered element
    height: 200,insetPadding: 20,store: {
        fields: ['name','amount'],data: [..] // data
    },smooth: true,axes: [{
      type: 'category',position: 'bottom',fields: ['name'],label: {
        fill: 'rgba(0,10,30,.75)',fontSize: 15
      },style : {
        strokeStyle : 'rgba(0,.2)'
      }
    }],series: [
    {
      type: 'line',fill: true,style: {
        fill: '#a2d5f2',fillOpacity: .6,stroke: '#00a1fd',strokeOpacity: .6,},tooltip: {
        trackMouse: true,renderer: (tooltip,model,item) => {
            const content = item.record.data.name + ': ' + item.record.data.amount
            //tooltip.setHtml(model.get(item.field));
            tooltip.setHtml(content)
        }
      },xField: 'name',yField: 'amount',marker: {
        type: 'circle',radius: 5,lineWidth: 2,fill: '#fff',fillOpacity: 1,renderer: (sprite,config,rendererData,index) => {
        let store = rendererData.store,storeItems = store.getData().items,previousRecord = storeItems[index],currentRecord = (index > 0 ? storeItems[index - 1] : previousRecord),current = currentRecord && parseFloat(currentRecord.data['amount']),previous = previousRecord && parseFloat(previousRecord.data['amount']),changes = {};
              
        switch (config.type) {
          case 'marker':
              if (index == 0) {
                  return null; // keep the default style for the first marker
              }
              changes.strokeStyle = (current >= previous ? '#00a1fd' : 'red');
              //changes.fillStyle = '#fff';
              //changes.fillOpacity = 1;
              //changes.lineWidth = 2;
              break;
          case 'line':
              changes.strokeStyle = (current >= previous ? '#00a1fd' : 'red');
              changes.lineWidth = 2;
              changes.fillStyle = (current >= previous ? '#a2d5f2' : 'red');
              changes.fillOpacity = (current >= previous ? 1 : .1);
              break;
        }
        return changes;
      }
    }]
  });
})