最佳实践:从 lambda 构建复杂成员

Good practice : construction of a complex member from a lambda

我的 类 中有时会有这种模式:

class Variant;
struct Foo
{
  int x; 
  int y;
};

struct Bar
{
  int first() const; 
  int second() const; 
};

class Bar
{
  public:
    Bar(Variant v)
    {
       // For brevity there are no 
       // tests in this sample code
       Bar aBar = v.get<Bar>();  // Variant::get is a slow operation
       m_theFoo.x = aBar.first();
       m_theFoo.x = aBar.second();
    }

  private:
    Foo m_theFoo;
};

我宁愿在初始化列表中初始化 m_theFoo。但是,我不想像这样调用 Variant::get 两次,因为这可能是一个缓慢的操作:

Bar(Variant v):
   m_theFoo{v.get<Bar>().first(), v.get<Bar>().second()}
{
}

所以我想到了这个:

Bar(Variant v):
    m_theFoo{[&] () { 
            Bar aBar = v.get<Bar>();
            return Foo{aBar.first(), aBar.second()};
        }()
    }  
{
}

基本上,初始化是由调用的 lambda 和 returns 初始化成员完成的。

我想知道这听起来是好是坏,这种初始化方法是否有缺点。例如,lambda 实例化的权重是多少?

让它成为这样的无状态 lambda 会更好吗?

Bar(Variant v):
    m_theFoo{[] (const Variant& v_sub) { 
            Bar aBar = v_sub.get<Bar>();
            return Foo{aBar.first(), aBar.second()};
        }(v)
    }  
{
}

只需将您的初始化转发给一个函数 - 您将获得所有好处,而不会因为您的代码有点神秘而看起来很奇怪:

Bar(Variant v)
: m_theFoo(makeFoo(v))
{ }

与:

Foo makeFoo(const Variant& v) {
    Bar theBar = v.get<Bar>();
    return Foo{theBar.first(), theBar.second()};
}