How to force a static member to be initialized?

后端 未结 5 1578
悲&欢浪女
悲&欢浪女 2020-11-27 05:46

Consider this example code:

template
char register_(){
    return D::get_dummy(); // static function
}

template
struct Foo{
           


        
5条回答
  •  佛祖请我去吃肉
    2020-11-27 05:57

    Something like that comes to my mind:

    // in some c++ file (to make i with internal linkage)
    static int i = init_dummy(Foo::dummy);
    

    where init_dummy is defined like this:

    int init_dummy(...)
    {
      return 1;
    }
    

    Due to variable args you can put more initializations there like:

    static int i = init_dummy(Foo::dummy, Foo::dummy, Foo::dummy);
    

提交回复
热议问题