简体   繁体   English

如何在main之前强制初始化静态局部变量?

[英]How to force the initialization of a static local variable before main?

Consider the pair of functions below: 考虑下面的一对函数:

double MYAPI foo(double x) { 
     return x; 
}
Register register_foo_([] { 
     return reg(&foo, "foo", ...); // function name repeated used
});

register_foo_ is a global variable, initialized before dllmain , whose constructor takes a lambda that repeatedly references the name of the function above it literally. register_foo_是一个全局变量,在dllmain之前初始化,其构造函数采用一个lambda,它重复地引用字面上方的函数名。 It would be great if the registration code can move inside the function above to reduce the chance of making an error. 如果注册代码可以在上面的函数内移动以减少出错的可能性,那将是很好的。 I tried: 我试过了:

double MYAPI foo(double x) { 
     static Register register_foo_([] { 
          return reg(&foo, "foo", ...); // static local does not initialize before dllmain
     });
     return x; 
}

If the above code works, then I can easily turn it into a macro that makes use of __FUNCNAME__ . 如果以上代码有效,那么我可以轻松将其转换为使用__FUNCNAME__的宏。 Is there a way to force the initialization of static local variable register_foo_ before dllmain? 有没有办法强制在dllmain之前初始化静态局部变量register_foo_

Static variables local to a function (method) are initialized on first use of the function they're in. (They're zero-initialized at program load, then initialized "properly" via code when the function is first entered.) See the answers to this question . 函数(方法)本地的静态变量在首次使用它们所在的函数时初始化。(它们在程序加载时初始化为零,然后在首次输入函数时通过代码“正确”初始化。)参见回答这个问题 So your proposed movement of that code into the function changes the semantics of initialization, and it won't work. 因此,您建议将该代码移动到函数中会改变初始化的语义,并且它将无法工作。

Your original code worked, so what you apparently wanted was to move the code inside the function so it was somehow tied closer together in your mind - or the mind of a reader of the code - so that you could see that your string constant name and function name were right. 您的原始代码有效,所以您显然想要的是将代码移动到函数内部,以便它在某种程度上紧密地联系在一起 - 或者代码的读者的头脑 - 以便您可以看到您的字符串常量名称和功能名称是对的。 Also maybe so that you could ensure the registration was done. 也许这样你就可以确保注册完成了。 And therefore what you want is to accomplish DRY. 因此你想要的是干涸。

The traditional (and only) way to accomplish that is by using a preprocessor macro that expands into the registration call and the function header. 传统(也是唯一)实现这一目标的方法是使用扩展到注册调用和函数头的预处理器宏。

You proposed using a macro yourself - now expand the macro so it not only generates the registration function but also the function header. 您建议自己使用宏 - 现在扩展宏,这样它不仅生成注册函数, 生成函数头。

This runs a function before main() , not sure if it will work for dllmain() : 这在main()之前运行一个函数,不确定它是否适用于dllmain()

#include <iostream>

int func_before_main()
{
    std::cout << "func_before_main()" << '\n';
    // do before main stuff
    return 0;
}

const int dummy = func_before_main();

int main()
{
    std::cout << "main()" << '\n';
}

Output: 输出:

func_before_main()
main()

I suppose you want to achieve a syntax similar to: 我想你想要实现类似于的语法:

DEFINE_FUNC(void, foo, (double x)) {
    return x;
}

... and have the boilerplate autogenerated. ...并自动生成样板。 That's actually very simple to do if you bring the Register above the function, with the help of a declaration: 如果你在一个声明的帮助下将Register放在函数之上,这实际上非常简单:

#define DEFINE_FUNC(Ret, name, args)   \
    Ret name args;                     \
    Register register_##name##_([] {   \
        return reg(&name, #name, ...); \
    });                                \
    Ret name args

No, there's not. 不,没有。 That's your answer. 那是你的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 静态局部变量和静态局部对象初始化 - static local variable and static local object initialization 全局变量的静态初始化是否在`main()`之前完成? - Is the static initialization of global variables completed before `main()`? 局部范围静态变量的零初始化和静态初始化 - zero initialization and static initialization of local scope static variable 在main中初始化静态类变量 - Initialization of static class variable inside the main 在静态初始化列表之前对变量进行预处理 - Preprocess a variable before static initialization list c ++在“本地范围”中初始化静态变量 - c++ initialization static variable in "local scope" 异常后重试本地静态变量初始化 - Retry local static variable initialization after exception 如何理解允许实现在某些情况下将非局部变量的动态初始化视为静态初始化? - How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases? C ++静态存储持续时间对象在main()之前初始化 - C++ Static storage duration objects initialization before main() 在模板类方法中,如何在共享库和主程序中使静态变量具有相同的初始化值? - How to make static variable have the same initialization value in shared library and in main program, in a templated class method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM