完美捕捉lambda中完美的货运代理(通用参考)

时间:2015-07-14 14:57:38

标签: c++ lambda perfect-forwarding

所以我有一个完美的转发器,我想在lambda中适当地捕获它,这样就可以复制R值,并通过引用捕获L值。然而,简单地使用std :: forward不能完成这项工作,如此代码所示:

#include<iostream>

class testClass
{
public:
   testClass() = default;
   testClass( const testClass & other ) { std::cout << "COPY C" << std::endl; }
   testClass & operator=(const testClass & other ) { std::cout << "COPY A" << std::endl; }
};

template< class T>
void testFunc(T && t)
   { [test = std::forward<T>(t)](){}(); }

int main()
{
   testClass x;
   std::cout << "PLEASE NO COPY" << std::endl;
   testFunc(x);
   std::cout << "DONE" << std::endl;

   std::cout << "COPY HERE" << std::endl;
   testFunc(testClass());
   std::cout << "DONE" << std::endl;
}

使用

进行编译
g++ -std=c++14 main.cpp

生成输出

PLEASE NO COPY
COPY C
DONE
COPY HERE
COPY C
DONE

在一个完美的世界里,我只想在右边的情况下出现“COPY C”,而不是左值的情况。

我的工作是使用一个辅助函数重载L-和R-值,但我想知道是否有更好的方法。

干杯!

1 个答案:

答案 0 :(得分:4)

您可以使用以下内容:

//Get points for bounding view
        //Pad = space between polygons
        //leftX = Left bound of parent view
        //rightX = Right bound of parent view
        //topY = Top bound of parent view
        //bottomY = Bottom bound of parent view
        //width = Width of a single polygon before transform
        //peak = How far a polygon will reach into the space of another polygon after transform (1/3 polygon width)
        //tip = peak with padding taken into account
        var context = UIGraphicsGetCurrentContext()
        var pad:CGFloat = (CGFloat)(Numbers.APPLICATIONPADDING)
        var leftX:CGFloat = (CGFloat)(self.bounds.origin.x)
        var rightX:CGFloat = (CGFloat)(self.bounds.size.width)
        var topY:CGFloat = (CGFloat)(self.bounds.origin.y)
        var bottomY:CGFloat = (CGFloat)(self.bounds.size.height)
        var width:CGFloat = (CGFloat)(self.bounds.width) / (CGFloat)(applications.count)
        var peak:CGFloat = width/3.0
        var tip:CGFloat = peak - pad

        //Draw all applications
        var appNum:CGFloat = 1.0
        var appCount:CGFloat = (CGFloat)(applications.count)
        var endTopX:CGFloat = 0.0
        var endBottomX:CGFloat = 0.0
        var temp:CGFloat!
        var textRightX:CGFloat = 0.0
        var textLeftX:CGFloat = 0.0
        var label:UILabel!
        for app in applications{
            if(app.color != nil){
                CGContextSetFillColorWithColor(context, app.color!.CGColor)
            }
            else{
                CGContextSetFillColorWithColor(context, Colors.PARTCOLORDEFAULT.CGColor)
            }
            //Each polygon will breach into the space of the next and the previous after transform by the tip with a given padding
            //Each text field will stretch across the middle of each polygon with a small padding (3) to prevent overreaching
            if(appNum == 1 && applications.count == 1){
                CGContextMoveToPoint(context, leftX, topY)
                CGContextAddLineToPoint(context, width, topY)
                CGContextAddLineToPoint(context, width, bottomY)
                CGContextAddLineToPoint(context, leftX, bottomY)
            }
            if(appNum == 1){
                CGContextMoveToPoint(context, leftX, topY)
                CGContextAddLineToPoint(context, width + tip, topY)
                CGContextAddLineToPoint(context, width - peak, bottomY)
                CGContextAddLineToPoint(context, leftX, bottomY)
                textLeftX = leftX + (leftX - leftX) / 2 + 3
                textRightX = (width - peak) + ((width + tip) - (width - peak)) / 2.0 - 10
                endTopX = width + tip
                endBottomX = width - peak
            }
            else if(appNum == appCount){
                CGContextMoveToPoint(context, endTopX + pad, topY)
                CGContextAddLineToPoint(context, rightX, topY)
                CGContextAddLineToPoint(context, rightX, bottomY)
                CGContextAddLineToPoint(context, endBottomX + pad, bottomY)
                textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2.0 + 10
                textRightX = rightX + (rightX - rightX) / 2.0 - 10
                endTopX = width
                endBottomX = width
            }
            else{
                CGContextMoveToPoint(context, endTopX + pad, topY)
                CGContextAddLineToPoint(context, width * appNum + tip, topY)
                CGContextAddLineToPoint(context, width * appNum - peak, bottomY)
                CGContextAddLineToPoint(context, endBottomX + pad, bottomY)
                textLeftX = (endBottomX + pad) + ((endTopX + pad) - (endBottomX + pad)) / 2 + 10
                textRightX = (width * appNum - peak) + ((width * appNum + tip) - (width * appNum - peak)) / 2 - 3
                endTopX = width * appNum + tip
                endBottomX = width * appNum - peak
            }
            CGContextFillPath(context)

            //Add text label
            label = UILabel(frame: CGRectMake(textLeftX, topY, (textRightX-textLeftX), bottomY))
            label.textAlignment = NSTextAlignment.Center
            label.text = app.baseSeries
            label.numberOfLines = 1
            label.adjustsFontSizeToFitWidth = true
            label.font = UIFont(name:"OpenSans-SemiBold", size:30)

            self.addSubview(label)
            appBounds.append(label.frame)

            appNum++
        }
        self.layer.borderColor = UIColor.lightGrayColor().CGColor
        self.layer.borderWidth = 1.0

Live Demo

但提供辅助功能似乎更具可读性