Created
July 26, 2013 14:14
-
-
Save breyed/6089195 to your computer and use it in GitHub Desktop.
C++ LINQ copy constructor demo. This is a modified version of CppLinq.Mini.cpp to include a call to `first` in [mrage__visual_cpp_bug_repro](https://cpplinq.codeplex.com/SourceControl/changeset/359ffa38b853c0113589b5cb69ff05d36e375a6a). It also includes an corresponding call to std::find_if. Only the call to first invokes the copy constructor of…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------------------------------------------------------------- | |
// Copyright (c) Mårten Rånge. | |
// ---------------------------------------------------------------------------------------------- | |
// This source code is subject to terms and conditions of the Microsoft Public License. A | |
// copy of the license can be found in the License.html file at the root of this distribution. | |
// If you cannot locate the Microsoft Public License, please send an email to | |
// [email protected]. By using this source code in any fashion, you are agreeing to be bound | |
// by the terms of the Microsoft Public License. | |
// ---------------------------------------------------------------------------------------------- | |
// You must not remove this notice, or any other, from this software. | |
// ---------------------------------------------------------------------------------------------- | |
#include "stdafx.h" | |
// ---------------------------------------------------------------------------------------------- | |
#include "../CppLinq/cpplinq.hpp" | |
int copyConstructorCalls; | |
class A | |
{ | |
public: int i; | |
public: explicit A(int i): i(i) {} | |
public: A(A const& a): i(a.i) { ++copyConstructorCalls; } | |
}; | |
// ---------------------------------------------------------------------------------------------- | |
int main (int argc, char* args[]) | |
{ | |
using namespace cpplinq; | |
std::vector<A> as; | |
as.reserve(4); | |
as.emplace_back(2); | |
as.emplace_back(3); | |
as.emplace_back(4); | |
as.emplace_back(5); | |
auto begin = as.begin(); | |
auto end = as.end(); | |
printf("Setup CC: %d\r\n", copyConstructorCalls); copyConstructorCalls = 0; | |
auto result = from_iterators (begin, end) | |
>> skip (1) | |
>> select ([] (A const& a) {return a.i < 5 ? a.i : 0;}) | |
>> sum () | |
; | |
printf ("%d\r\n", result); | |
printf("Select CC: %d\r\n", copyConstructorCalls); copyConstructorCalls = 0; | |
A const& resultFromFirst = from_iterators(begin, end) >> skip(1) >> first( [] (A const& a) {return a.i < 5;} ); | |
printf ("%d\r\n", resultFromFirst.i); | |
printf("First CC: %d\r\n", copyConstructorCalls); copyConstructorCalls = 0; | |
A const& resultFromFindIf = *std::find_if(begin + 1, end, [] (A const& a) {return a.i < 5;}); | |
printf ("%d\r\n", resultFromFindIf.i); | |
printf("find_if CC: %d\r\n", copyConstructorCalls); copyConstructorCalls = 0; | |
return 0; | |
} | |
// ---------------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment