在函数调用中定义函数 [英] Defining function inside function call

查看:100
本文介绍了在函数调用中定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想法来节省时间,包括创建一个临时函数以用作需要它的函数的参数。我之所以这么做,是因为我想用一种简单的方式(使用Win32 API)在新线程中执行某些操作,而无需定义我将要使用的所有函数。



下面是一个例子:

  void msg(const string& message){
MessageBox(0,message, .c_str(),Message,0);
}

这会产生一个消息框,但程序会暂停,直到它关闭。解决方案是为消息框创建一个与主线程同时运行的线程。

  void msg(const string&消息){
CreateThread(0,0,
(LPTHREAD_START_ROUTINE)({MessageBox(0,message.c_str(),Message,0);}),
0,0, 0);

$ / code>

在这种情况下, LPTHREAD_START_ROUTINE 定义为

typedef DWORD(* LPTHREAD_START_ROUTINE)(LPVOID param);



因为我有多个函数需要另一个线程来达到这个目的,所以将函数放入对 CreateThread 的调用中似乎效果不错。

但是我说要使用 LPVOID param 。我想知道这种方法的标准,以及我可以在哪里找到如何使用它来获得更高级的技术。另外,我知道在函数中使用它,以便将来使用它(例如,可以添加消息来处理消息并调用相应函数的消息循环函数)是一个坏主意,因为该函数是临时的,不会是能够在需要时被调用。除了线程之外,是否真的有其他用途,例如为了使用它而在其他地方使用一行代码的功能令人讨厌?

称为lambda。它们对于超出此目的的许多用途非常有用,并且在C ++ 11标准中。你可以在最新的GCC和MSVC中找到它们。但是,MSVC当前的实现不允许转换为函数指针,因为当时Standard没有指定这种转换。 VC11将执行此转换。此代码符合标准的C ++ 11:

  void msg(const string& message){
CreateThread (0,0,
[](LPVOID * param){MessageBox(0,message.c_str(),Message,0);},
0,0,0);
}


I had an idea to save time involving creating a temporary function to use as an argument to a function that needs it. The reason I'm after this behaviour is to do things in a new thread in an easy manner (using Win32 API) without having to define all kinds of functions I'll use.

Here's an example:

void msg (const string & message) {
    MessageBox (0, message.c_str(), "Message", 0);
}

This will produce a message box, but your program is halted until it closes. The solution is to create a thread for the message box that runs concurrently with the main thread.

void msg (const string & message) {
    CreateThread (0, 0, 
    (LPTHREAD_START_ROUTINE)({MessageBox (0, message.c_str(), "Message", 0);}), 
    0, 0, 0);
}

In this case, LPTHREAD_START_ROUTINE is defined as
typedef DWORD (*LPTHREAD_START_ROUTINE)(LPVOID param);

Since I have multiple functions wanting another thread for a purpose like that, putting the function in the call to CreateThread seems to be working well.

But say I wanted to use that LPVOID param. I'd like to know how standard this method is, and where I can find out how to use it for more advanced techniques. Also, I know using it in a function that will store it for later use (eg. a message loop function where you can add messages to handle and a corresponding function to call) is a bad idea as the function is temporary and would not be able to be called when needed. Is there really any use beyond things like threads where it's annoying to make one line functions elsewhere in order to use it?

解决方案

It's called a "lambda". They are very useful for many purposes beyond this and are in the C++11 Standard. You can find them in the latest GCC and MSVC. However, MSVC's current implementation does not permit conversion to a function pointer, as the Standard did not specify such a conversion at that time. VC11 will implement this conversion. This code is Standard-conforming C++11:

void msg (const string & message) {
    CreateThread (0, 0, 
    [](LPVOID* param) { MessageBox (0, message.c_str(), "Message", 0); }, 
    0, 0, 0);
}

这篇关于在函数调用中定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆