1

I have a question about the c++ initializer list.

I have a class with const structs that need to be initialized in the initializer list, because they're const. This works perfectly this way:

bglib::bglib()
    : ble_class_evt_handlers(   {{ble_class_system_evt_handlers,7},
                                {ble_class_flash_evt_handlers,1},
                                {ble_class_attributes_evt_handlers,3},
                                {ble_class_connection_evt_handlers,5},
                                {ble_class_attclient_evt_handlers,7},
                                {ble_class_sm_evt_handlers,5},
                                {ble_class_gap_evt_handlers,2},
                                {ble_class_hardware_evt_handlers,4},
                                {NULL,0},
                                {ble_class_dfu_evt_handlers,1},
                                } ),
      ble_class_rsp_handlers(   {{ble_class_system_rsp_handlers,18},
                                {ble_class_flash_rsp_handlers,9},
                                {ble_class_attributes_rsp_handlers,6},
                                {ble_class_connection_rsp_handlers,9},
                                {ble_class_attclient_rsp_handlers,12},
                                {ble_class_sm_rsp_handlers,8},
                                {ble_class_gap_rsp_handlers,11},
                                {ble_class_hardware_rsp_handlers,21},
                                {ble_class_test_rsp_handlers,7},
                                {ble_class_dfu_rsp_handlers,4},
                                }  )

{}

The cpp header file contains this:

const struct ble_class_handler_t ble_class_evt_handlers[ble_cls_last];
const struct ble_class_handler_t ble_class_rsp_handlers[ble_cls_last];

Now my question is: Is there any possible way to move the init functions to another file, which i refer to in the initializer list of the bglib class? For example, the init list calls a method that initializes the arrays (i know its not possible to call methods from the init list but just to make clear what i want to do)

Reason for this is that i need to initialize a whole lot more arrays like this, and i think its ugly to have an initializer lists that contains hundreds of lines of code.

PS. I know its ugly to use const arrays this way in c++, but i'm including a C library into a C++ project, and i dont have time to completely rewrite the C library.

Thanks in advance!

3
  • 1
    Probably you can include a header, which #define your lists. Sep 17, 2015 at 12:40
  • What compiler are you using? I can't get this code compiled: 'bglib::ble_class_evt_handlers': array initialization requires a brace-enclosed initializer list Sep 17, 2015 at 13:26
  • I'm using MSVC10 under Qt.
    – Jan Roorda
    Sep 17, 2015 at 13:30

1 Answer 1

2

Have you (would you) consider the following:

bglib::bglib(): 
   ble_class_evt_handlers( 
#include  "ble_class_evt_handlers_init.inc"
                         ),
  ble_class_rsp_handlers( 
#include  "ble_class_system_rsp_handlers_init.inc"
                        )
{}

or perhaps

bglib::bglib() : 
#include "bglib_init.inc"
{}
1
  • I have tried that, but unfortunately it does not work.
    – Jan Roorda
    Sep 18, 2015 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.