C++ 在main之前调用函数
是否可以在输入C++ 在main之前调用函数,c++,function,callback,main,C++,Function,Callback,Main,是否可以在输入main之前立即注册要运行的功能?我知道所有全局对象都是在进入main之前创建的,因此我可以将代码放入全局对象的构造函数中,但这并不保证任何特定顺序。我想做的是在构造函数中放入一些注册码,但是,唉,我不知道该在那里放什么:)我想这是高度系统特定的?不确定这正是您想要的。。。但它应该能做到这一点 int main() { static int foo = registerSomething(); } 最好在main或first-access中显式调用此类注册函数(但如果您是多线
main
之前立即注册要运行的功能?我知道所有全局对象都是在进入main
之前创建的,因此我可以将代码放入全局对象的构造函数中,但这并不保证任何特定顺序。我想做的是在构造函数中放入一些注册码,但是,唉,我不知道该在那里放什么:)我想这是高度系统特定的?不确定这正是您想要的。。。但它应该能做到这一点
int main() {
static int foo = registerSomething();
}
最好在main或first-access中显式调用此类注册函数(但如果您是多线程的,first-access init可能会带来问题)。如果您使用的是
gcc
,则可以在函数上使用构造函数
属性,在main
之前调用它(有关更多详细信息,请参阅)
constructor
析构函数
constructor
属性使函数在执行进入main()
之前自动调用。类似地,destructor
属性使函数在main()
完成或调用exit()
后自动调用。具有这些属性的函数对于初始化将在程序执行期间隐式使用的数据非常有用
我在这里猜测,但是:
typedef std::map<std::string, std::string> RegistrationCache;
RegistrationCache& get_string_map()
{
static RegistrationCache cache;
return cache;
}
class Registration
{
Registration(std::string name, std::string value)
{
get_string_map()[name] = value;
}
};
typedef std::map RegistrationCache;
注册缓存&获取字符串映射()
{
静态注册缓存;
返回缓存;
}
班级注册
{
注册(标准::字符串名称,标准::字符串值)
{
获取字符串映射()[name]=值;
}
};
目标
假设您想要以下内容:
实施
C++版本-静态变量+构造函数:
C版本-静态变量+函数
笔记
- 警告:我还没有在正确的纯C编译器上测试“C”版本。祈求好运;如果不起作用,就贴一张便条
- 警告:编译器的可移植性通常是一件棘手的事情。如果其他编译器出现错误,我不会感到震惊
- BOOST_PP_CAT代码从中被盗。我简化了实现,在此过程中可能会影响可移植性。如果不起作用,请尝试原始的(更详细的)实现,并在下面发表评论。或者,如果您已经在使用Boost,您可以只使用他们的版本
- 如果您试图理解Boost魔法,请注意(至少对我来说,在本场景中),以下内容似乎也适用:
您能否将代码“在输入
main
之前立即运行”放入main()
中,然后创建第二个函数,real\u main()
,从那里调用它?也许这是欺骗。声明:不保证任何特定的顺序是总体上通用的,因此不完全正确。编译单元的构造顺序被定义为声明顺序。只是编译单元之间的顺序没有得到很好的定义(因为它取决于链接器)。这个函数在main
中调用的第一个函数中无法实现,您到底想用它实现什么?这与只使用registerSomething()
而不使用静态变量没有多大区别。不,但它可以(通过一些强制)可以说是在main中的代码之前逻辑执行:)oops。我的第一次尝试失败了。StaticExecute\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。用从Boost偷来的疯狂魔法修复它。
STATIC_EXECUTE {
printf("This probably prints first"\n");
}
STATIC_EXECUTE {
printf("But order isn't guaranteed in the language spec, IIRC"\n");
}
int main(int argc, char **argv) {
printf("This definitely prints last. Buh Bye.\n");
}
// This is some crazy magic that produces __StaticExecute__247
// Vanilla interpolation of __StaticExecute__##__LINE__ would produce __StaticExecute____LINE__
// I still can't figure out why it works, but it has to do with macro resolution ordering
// If you already have Boost, you can omit this part
#define BOOST_PP_CAT(a, b) BOOST_PP_CAT_I(a, b)
#define BOOST_PP_CAT_I(a, b) BOOST_PP_CAT_II(~, a ## b)
#define BOOST_PP_CAT_II(p, res) res
// This avoids repeating the BOOST_PP_CAT 5X
#define STATIC_EXECUTE \
STATIC_EXECUTE_I(BOOST_PP_CAT(__StaticExecute__, __LINE__))
// This is the meat, a static instance of a class whose constructor runs your code
#define STATIC_EXECUTE_I(uniq_name) \
static struct uniq_name { \
uniq_name(); \
} BOOST_PP_CAT(uniq_name, __var); \
uniq_name::uniq_name() // followed by { ... }
// ...
// The meat: a static variable initialized from a function call
#define STATIC_EXECUTE_I(uniq_name) \
static void uniq_name (); \
static int BOOST_PP_CAT(uniq_name, __var) = \
(uniq_name(), 0); \
static void uniq_name() // followed by { ... }
#define BOOST_PP_CAT(a, b) BOOST_PP_CAT_I(a, b)
#define BOOST_PP_CAT_I(a, b) a ## b