是否可以将C++语句分解如下?

huangapple go评论142阅读模式
英文:

Is it possible to breakdown a c++ statement as shown?

问题

这个语句是从使用了矢量Autosar自适应Davinci API/函数等开发的源文件中提取的。

adaptive::communication::ServiceHandle find_a_service =
services::nameofservice::proxy::serviceproxy::StartSearchService(
[this](const adaptive::communication::ContainerServiceHandles<services::nameofservice::proxy::serviceproxy::HandleTypes>& handles) 
{ 
    SearchServiceHandler(handles);
}, adaptive::core::InstanceSpec{ kInstance });

我已经重新命名/模糊了名称,但在源代码中保持了格式不变。有人能帮忙将这个复杂的语句分解成几个简单的语句吗?

谢谢。

英文:

The statement is taken from a source file that is developed using vector autosar adaptive davinci APIs/functions etc.

adaptive::communication::ServiceHandle find_a_service = services::nameofservice::proxy::serviceproxy::StartSearchService(
    [this](const adaptive::communication::ContainerServiceHandles&lt;services::nameofservice::proxy::serviceproxy::HandleTypes&gt;&amp; handles) 
    { 
        SearchServiceHandler(handles);
    }, adaptive::core::InstanceSpec{ kInstance });

I have renamed/obscured names but kept the format intact in the source code. Can anyone help in breaking down this complex statement to several simple ones?

Thanks.

答案1

得分: 2

以下是翻译好的部分:

在为每个名称创建别名时,这是一个相当简单的语句,以提高可读性格式化:

ServiceHandler find_a_service = 
        StartSearchService([this](const HandleContainer<Htypes>& handles) {
            SearchServiceHandler(handles);
        }, 
        InstanceSpec{kInstance});

它声明了一个名为`find_a_service`的变量,由某个表达式初始化。目前不清楚`StartSearchService`是一个类型还是一个函数,或者是一个可调用对象的名称。

`StartSearchService`的第一个参数是一个Lambda表达式,它捕获了`this`,以`HandleContainer<Htypes>`的const引用作为参数,并调用某个*可调用*对象并将其作为参数传递。

`StartSearchService`的第二个参数是一个临时值,是由类型为`InstanceSpec`的表达式初始化的,该表达式使用列表`{kInstance}`初始化,不管这些是什么。

现在你可以*小心地*为每个名称创建别名。你可以创建类型别名,也可以使用using声明。要小心,因为你不希望发生冲突。

// 类型别名
using ServiceHandler  = adaptive::communication::ServiceHandle;
using HandleContainer = adaptive::communication::ContainerServiceHandles;

// 等等。

使用声明会将完全限定的名称缩短为较短的名称:

// 在函数作用域中:
using adaptive::communication::ContainerServiceHandles;
// 现在你可以写ContainerServiceHandles了,但是任何同名的实体现在都是模棱两可的。

最后,基本的using-namespace(仅在communication是一个命名空间的情况下有效):

using namespace adaptive::communication;
// 命名空间adaptive::communication中的所有名称现在在本地是有效的。可能会出现歧义。

这种方法并不是没有缺陷的。在模板代码或前向声明中,甚至可能无法以这种形式使用。Koenig的名称查找和必须在使用之前声明的命名空间的必要性都会引发问题

在大多数情况下,您可以将Lambda单独编写,但这是不建议的,因为这样会创建副本,并且原始副本将一直存在到作用域的末尾,这可能会a)损害优化b)可能会产生副作用,有关这些实体的一切都是未知的。

auto searchPredicate = 
        [this](const HandleContainer<Htypes>& handles ) {
            SearchServiceHandler(handles);
        };

注意:上述翻译已经将代码部分略去,只包含代码中的注释和说明性文字。

英文:

While aliasing every name I can, this is rather simple statement, formatted for readability:

ServiceHandler find_a_service = 
			StartSearchService ( [this](const HandleContainer&lt;Htypes&gt;&amp; handles ) {
					SearchServiceHandler(handles);
			}, 
			InstanceSpec{kInstance} );

It declares some variable find_a_service, intialized by some expression. It's unknown that StartSearchService is a type or a function, or a name of callable object.

First argument of StartSearchService argument is a lambda expression, which captures this, takes a const reference to HandleContainer&lt;Htypes&gt; as a parameter, and calls some callable with it as argument.

Second argument of StartSearchService is a temporal value, resulting from an expression of type InstanceSpec, initialized by list {kInstance}, whatever these are.

Now you can carefully create aliases for each name. You can create type alisases or you can use using declaration.Carefully because you do not want conflicts.

//type alias
using ServiceHandler  = adaptive::communication::ServiceHandle;
using HandleContainer = adaptive::communication::ContainerServiceHandles;

// etc.

Using declarion would cut fully qualified name to short one:

// in function scope:
using adaptive::communication::ContainerServiceHandles;
// you can write ContainerServiceHandles now, but any samely named entity is ambigous now.

Finally, fundamental using-namespace (valid ONLY if communication is a namespace):

using namespace adaptive::communication;
// all names in namespace adaptive::communication 
// are now valid in local. Ambiguity is possible.

The approach isn't without flaws. It may not be even possible to use in such form in template code or with forward declarations. Koenig's name lookup and necessity of namespace to be declared before arise their ugly heads.

You may write lambda separate in most cases, but its ill-advised, because you would create a copy of that and original would live until end of scope, which would a) harm optimization b) can have side-effects, everything about those entities is unknown.

auto searchPredicate = 
		[this](const HandleContainer&lt;Htypes&gt;&amp; handles ) {
			SearchServiceHandler(handles);
	 	};

huangapple
  • 本文由 发表于 2023年8月10日 17:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定