如何从函数中获得替代值,通过非const输出参数赋予所需的数据,以便为其分配引用到const的变量? [英] How to get alternative value from function that gives wanted data via non-const output parameter for assigning reference-to-const variable to it?

查看:233
本文介绍了如何从函数中获得替代值,通过非const输出参数赋予所需的数据,以便为其分配引用到const的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注释的代码工作,但它不是一个参考,所以它有更多的计算成本。

The commented code works, but it is not a reference, so it has more computational cost.

void CClass::Function(const CArray<CItem*>* ItemsInput) const
{
    /*
    CArray<CItem*> Items;

    if (ItemsInput != nullptr)
        Items.Copy(*ItemsInput);
    else
        GetContainer().GetInnerItems(Items, NULL, true);
    */


    const CArray<CItem*>& Items=
        (ItemsInput!= nullptr)?
            *ItemsInput
        :
        [this] () -> const CArray<CItem*> 
        {
            CArray<CItem*> InnerItems;
            GetContainer().GetInnerItems(InnerItems, NULL, true);
            return const_cast <const CArray<CItem*>& > (InnerItems);
        }
    ;

    //...
 }

取消注释的代码是我尝试使用三元运算符和lambda表达式的方法的最大程度,但是直到现在它是不成功的,给出错误:

The uncommented code is the maximal extent where I got trying to use an approach of ternary operator and lambda expression, but until now it was unsuccessful, giving the error:

1>Class.cpp(line of the last semicolon): error C2446: ':' : no conversion from '`anonymous-namespace'::<lambda2>' to 'const CArray<TYPE>'
1>          with
1>          [
1>              TYPE=CItem *
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

重要要注意的事情:


  • 我在lambda捕获时传递 this 访问 GetInnerItems 方法

  • GetInnerItems 它是 CArray 类型的第一个参数,它是非常量,永远不能为const。

  • I pass this on the lambda capture, just to have access to the GetInnerItems method
  • The GetInnerItems method gives the inner items via its first parameter of type CArray<CItem*>&, which is non-const and could never be const.

因此,在我看来,问题是 GetInnerItems 的工作方式,与三元运算符无关。假设我不能改变它的签名返回一个 CArray ,因为 CArray 派生自 CObject 其中声明:

Therefore, it seems to me the problem is the way the GetInnerItems works and has nothing to do with ternary operator. Assume I can not change its signature to return a CArray, because CArrayis derived from CObject which declares:

class AFX_NOVTABLE CObject
{
//...
private:
    CObject(const CObject& objectSrc);              // no implementation
    void operator=(const CObject& objectSrc);       // no implementation
//...
}

are:


  • 如何将替代值指定给 reference-to-const 变量保持const正确性?

  • How can I assign the alternative value to the reference-to-const Items variable keeping const-correctness?

如果您成功地使用

If you successfully answered the first question using a const_cast, can you now give a solution without it?

推荐答案

我声明了lambda函数,但我忘了调用它。因此,需要括号。因此,请注意在声明后调用其执行的()

I declared the lambda-function, but I forget to call it. So, parentheses are needed. Therefore, notice the () after the declaration with the goal of calling for its execution:

const CArray<CItem*>& Items=
    (ItemsInput!= nullptr)?
        *ItemsInput
    :
        [this]() -> const CArray<CItem*>&
        {
            CArray<CItem*> InnerItems;
            GetContainer().GetInnerItems(InnerItems, NULL, true);
            return (InnerItems);
        } ()
;

现在编译正常。

但这会导致一个新问题: Lambda没有自动推导返回类型

But this lead to a new question: Lambda did not automatically deduce return type

这篇关于如何从函数中获得替代值,通过非const输出参数赋予所需的数据,以便为其分配引用到const的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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