C ++缩短后续函数调用 [英] C++ Shorten subsequent function calls

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

问题描述

我尝试在C ++ 17中找到一种方法来缩短以下函数调用:

I try to find a way in C++17 to shorten following function calls:

GetCurrentContext()->GetEntityManager()->CreateEntity();

也许是这样的:

EM()->CreateEntity();

我尝试了一个函数指针,但这仅对静态函数有效:

I tried a function pointer, but this is only valid for static functions:

constexpr auto &EM = GetContext()->GetEntityManager;
// error: reference to non-static member function must be called;

有没有比使用宏更好的解决方案?

Is there a better solution than using a Macro?

#define EM GetContext()->GetEntityManager

使用内联函数调用时,恐怕编译器将忽略此操作,并且会产生不必要的开销:

When using an inline function call, i'm afraid, that the compiler will ignore this and i have an unnessessary overhead:

inline EntityManager* EM() { return GetCurrentContext->GetEntityManager(); }

这似乎也是错误的方法,因为我正在寻找别名,而不是要定义另一个函数.

Also this seems to be the wrong way to go, because i'm looking for an alias, not for another function to define.

每个上下文都有一个EntityManager,并且当前上下文可以在运行时更改.因此,我实际上是在寻找别名,而不是指向函数返回内容的const指针.

Every Context has an EntityManager and the current Context can change during runtime. So i'm really looking for an alias, not for a const pointer to what the function returns.

更新:

我发现了问题.使用自动返回类型时,内联函数变得与原始返回类型无关.即使将来会更改原始功能,也无需再次触摸别名.并信任编译器优化,这将真正成为真正的别名.

I found this Question. With return type auto, the inline function becomes independet from the original return type. Even if the original function will get changed in future, the alias need not be touched again. And with trust in compiler optimization this will really become a real alias.

因此,我认为(考虑到答案和评论)最好的解决方案是执行以下操作:

So i think (In consideration of the answers and comments) the best solution is to do the following:

inline decltype(auto) EM() { return GetCurrentContext()->GetEntityManager(); }

推荐答案

  1 #include <iostream>
  2 #include <string>
  3
  4 using namespace std;
  5
  6 class A {
  7   public:
  8    std::string x;
  9    A () {
 10      x += "a";
 11    }
 12
 13    std::string ax() {  //imagine this to be some object.
 14     return x;
 15    }
 16 };
 17
 18 int main () {
 19   A a;
 20   auto y = [] (decltype(a)& a) {
 21     return [&a] () {
 22       return a.ax(); //imagine this to be a big chain like yours. I just wanted to give you a working example, so I'm not doing your calls.
 23     };
 24   };
 25   std::cout << y(a)().at(0) << std::endl; //TADA!! this works
 26   return 1;
 27 }

这可以简化,我留给你.我只是在说明如何别名(甚至不知道类型).

This can be simplified, and I will leave it upto you. I'm just illustrating how to alias, (without even knowing the types).

您可以对函数指针执行类似的操作,然后根据需要为任意多个步骤添加别名,然后在所需的任何阶段返回.

you can do something similar with function pointers and then alias as many steps as you like and return at whichever stage you want.

这个想法是在另一个lambda里面包含一个 lambda ,这样您就不必担心对象已更改,因为它将进行转发而不存储对象.这样,在每次调用lamba时都会调用链中的所有函数,因为它会返回您所调用的内部lambda.

The idea is of having a lambda inside another lambda, this way you do not have to worry that the object changed since it is going to do forwarding not store the object. This way all functions in the chain are called at each invocation of the lamba as it returns the inner lambda that you invoke.

如果您仔细考虑,可以存储该函数,而无需执行 y(a),它是匆忙编写的.

If you think carefully, you can store the function, you don't need to do y(a), it was written in a rush.

这篇关于C ++缩短后续函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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