Skip to content
Snippets Groups Projects
Commit 15ae0f30 authored by Santiago Ospina De Los Ríos's avatar Santiago Ospina De Los Ríos Committed by Christoph Grüninger
Browse files

Include test for identity

parent 7affda68
No related branches found
No related tags found
1 merge request!696Support std::identity
......@@ -306,6 +306,9 @@ dune_add_test(SOURCES singletontest.cc
dune_add_test(SOURCES sllisttest.cc
LABELS quick)
dune_add_test(SOURCES stdidentity.cc
LABELS quick)
dune_add_test(SOURCES stdapplytest.cc
LINK_LIBRARIES dunecommon
LABELS quick)
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dune/common/std/functional.hh>
#include <iostream>
#include <cassert>
struct Foo {
static int count;
Foo() { ++count; std::cout << "construct" << std::endl; }
Foo(const Foo&) { ++count; std::cout << "copy construct" << std::endl; }
Foo(Foo&&) { ++count; std::cout << "move construct" << std::endl; }
~Foo() { --count; std::cout << "deconstruct" << std::endl; }
};
int Foo::count = 0;
template<typename T>
T&& assert_count(T&& arg, int count)
{
std::cout << std::decay_t<T>::count << std::endl;
assert(std::decay_t<T>::count == count);
return std::forward<T>(arg);
}
int main()
{
auto id = Dune::Std::identity();
assert_count(id(Foo()),1); // pass an r-value to identity, still constructed on the assert
const auto& foo0 = id(Foo()); // pass an r-value to identity
assert_count(foo0,0); // id(Foo()) is alredy doconstructed at this point
auto foo1 = id(Foo()); // pass an r-value to identity and move it to foo1
assert_count(foo1,1); // foo0 is alredy doconstructed at this point
Foo foo2;
assert_count(id(foo2),2); // pass an l-value to identity
const auto& foo3 = id(foo2); // pass an l-value to identity
assert_count(foo3,2); // foo still exist at this point
auto foo4 = id(foo2); // pass an l-value to identity and copy its result
assert_count(foo4,3); // copy of foo still exist at this point
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment