Home > front end >  How to initialize a structure through a inline constructors in C which has one member element is a
How to initialize a structure through a inline constructors in C which has one member element is a

Time:09-28

I am trying to initialize a structure through an inline constructor in C which has one member element is an array. In the following example is it possible to initialize _dmac array in MplsTunnel_() constructor call?

typedef struct MplsTunnel_
{
    uint32_t    _port;
    uint32_t    _iflIndex;      
    uint16_t    _vsi;
    int         _vcEncapAccess;
    uint8_t     _dmac[6];
    MplsTunnel_() : _port(0), _iflIndex(0), _vsi(0),  _vcEncapAccess(0) {}
    
} MplsTunnel_t;

CodePudding user response:

A fixed size array parameter should be passed like this:

std::uint8_t(&dmac)[6] 

The the rest of the example then becomes like this:

#include <cstdint>
#include <algorithm>

struct MplsTunnel_
{
    MplsTunnel_(const std::uint32_t port, const std::uint32_t iflIndex, const std::uint16_t vsi, const int vcEncapAccess, const std::uint8_t(&dmac)[6]) :
        _port{ port },
        _iflIndex{ iflIndex },
        _vsi{ vsi },
        _vcEncapAccess{ vcEncapAccess }
    {
        std::copy(std::begin(dmac), std::end(dmac), std::begin(_dmac));
    }

    std::uint32_t    _port;
    std::uint32_t    _iflIndex;
    std::uint16_t    _vsi;
    int              _vcEncapAccess;
    std::uint8_t     _dmac[6];
};

and then you can initialize a tunnel like this :

int main()
{
    MplsTunnel_ tunnel(80, 4u, 0, 0, { 32, 20, 10, 12, 24, 33 });
}

Note replacing _dmac with a std::array type will make the code cleaner :

#include <array>
#include <cstdint>

struct mpls_tunnel_t
{
    mpls_tunnel_t(const std::uint32_t port, const std::uint32_t iflIndex, const std::uint16_t vsi, const int vcEncapAccess, const std::array<std::uint8_t,6>& dmac) :
        _port{ port },
        _iflIndex{ iflIndex },
        _vsi{ vsi },
        _vcEncapAccess{ vcEncapAccess },
        _dmac{ dmac }
    {
    }

    std::uint32_t    _port;
    std::uint32_t    _iflIndex;
    std::uint16_t    _vsi;
    int              _vcEncapAccess;
    std::array<std::uint8_t, 6> _dmac;
};


int main()
{
    mpls_tunnel_t tunnel(80, 4u, 0, 0, { 32, 20, 10, 12, 24, 33 });
}

CodePudding user response:

A little bit orthogonal to the question as asked, but std::array<> are generally preferable to arrays and let you treat them as you would any other value type:

struct MplsTunnel_
{
    uint32_t               _port;
    uint32_t               _iflIndex;      
    uint16_t               _vsi;
    int                    _vcEncapAccess;
    std::array<uint8_t, 6> _dmac;

    MplsTunnel_(const std::array<uint8_t, 6>& dmac) 
      : _port(0), 
        _iflIndex(0), 
        _vsi(0),  
        _vcEncapAccess(0), 
        _dmac{dmac} {}
    
};

However, in the specific case asked by OP, still list-initializing the array members in the initialization list works just fine:

typedef struct MplsTunnel_
{
    uint32_t    _port;
    uint32_t    _iflIndex;      
    uint16_t    _vsi;
    int         _vcEncapAccess;
    uint8_t     _dmac[6];
    MplsTunnel_() : _port(0), _iflIndex(0), _vsi(0),  _vcEncapAccess(0), _dmac{1, 2, 3, 4, 5, 6} {}
    
} MplsTunnel_t;
  • Related