Move test into test/std subdirectory.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@224658 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
80
test/std/utilities/function.objects/refwrap/binary.pass.cpp
Normal file
80
test/std/utilities/function.objects/refwrap/binary.pass.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// check for deriving from binary_function
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
class functor2
|
||||
: public std::binary_function<char, int, double>
|
||||
{
|
||||
};
|
||||
|
||||
class functor3
|
||||
: public std::unary_function<int, int>,
|
||||
public std::binary_function<char, int, double>
|
||||
{
|
||||
public:
|
||||
typedef float result_type;
|
||||
};
|
||||
|
||||
class functor4
|
||||
: public std::unary_function<int, int>,
|
||||
public std::binary_function<char, int, double>
|
||||
{
|
||||
public:
|
||||
};
|
||||
|
||||
struct C
|
||||
{
|
||||
typedef int argument_type;
|
||||
typedef int result_type;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
static_assert((!std::is_base_of<std::binary_function<int, char, int>,
|
||||
std::reference_wrapper<functor1> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<char, int, double>,
|
||||
std::reference_wrapper<functor2> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<char, int, double>,
|
||||
std::reference_wrapper<functor3> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<char, int, double>,
|
||||
std::reference_wrapper<functor4> >::value), "");
|
||||
static_assert((!std::is_base_of<std::binary_function<int, int, int>,
|
||||
std::reference_wrapper<C> >::value), "");
|
||||
static_assert((!std::is_base_of<std::binary_function<int, int, float>,
|
||||
std::reference_wrapper<float ()> >::value), "");
|
||||
static_assert((!std::is_base_of<std::binary_function<int, int, float>,
|
||||
std::reference_wrapper<float (int)> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<int, int, float>,
|
||||
std::reference_wrapper<float (int, int)> >::value), "");
|
||||
static_assert((!std::is_base_of<std::binary_function<int, int, float>,
|
||||
std::reference_wrapper<float(*)()> >::value), "");
|
||||
static_assert((!std::is_base_of<std::binary_function<int, int, float>,
|
||||
std::reference_wrapper<float(*)(int)> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<int, int, float>,
|
||||
std::reference_wrapper<float(*)(int, int)> >::value), "");
|
||||
static_assert((!std::is_base_of<std::binary_function<C*, int, float>,
|
||||
std::reference_wrapper<float(C::*)()> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<C*, int, float>,
|
||||
std::reference_wrapper<float(C::*)(int)> >::value), "");
|
||||
static_assert((std::is_base_of<std::binary_function<const volatile C*, int, float>,
|
||||
std::reference_wrapper<float(C::*)(int) const volatile> >::value), "");
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// operator T& () const;
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test(T& t)
|
||||
{
|
||||
std::reference_wrapper<T> r(t);
|
||||
T& r2 = r;
|
||||
assert(&r2 == &t);
|
||||
}
|
||||
|
||||
void f() {}
|
||||
|
||||
int main()
|
||||
{
|
||||
void (*fp)() = f;
|
||||
test(fp);
|
||||
test(f);
|
||||
functor1 f1;
|
||||
test(f1);
|
||||
int i = 0;
|
||||
test(i);
|
||||
const int j = 0;
|
||||
test(j);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// reference_wrapper& operator=(const reference_wrapper<T>& x);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test(T& t)
|
||||
{
|
||||
std::reference_wrapper<T> r(t);
|
||||
T t2 = t;
|
||||
std::reference_wrapper<T> r2(t2);
|
||||
r2 = r;
|
||||
assert(&r2.get() == &t);
|
||||
}
|
||||
|
||||
void f() {}
|
||||
void g() {}
|
||||
|
||||
void
|
||||
test_function()
|
||||
{
|
||||
std::reference_wrapper<void ()> r(f);
|
||||
std::reference_wrapper<void ()> r2(g);
|
||||
r2 = r;
|
||||
assert(&r2.get() == &f);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
void (*fp)() = f;
|
||||
test(fp);
|
||||
test_function();
|
||||
functor1 f1;
|
||||
test(f1);
|
||||
int i = 0;
|
||||
test(i);
|
||||
const int j = 0;
|
||||
test(j);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// reference_wrapper(const reference_wrapper<T>& x);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test(T& t)
|
||||
{
|
||||
std::reference_wrapper<T> r(t);
|
||||
std::reference_wrapper<T> r2 = r;
|
||||
assert(&r2.get() == &t);
|
||||
}
|
||||
|
||||
void f() {}
|
||||
|
||||
int main()
|
||||
{
|
||||
void (*fp)() = f;
|
||||
test(fp);
|
||||
test(f);
|
||||
functor1 f1;
|
||||
test(f1);
|
||||
int i = 0;
|
||||
test(i);
|
||||
const int j = 0;
|
||||
test(j);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// reference_wrapper(T&&) = delete;
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::reference_wrapper<const int> r(3);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// reference_wrapper(T& t);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test(T& t)
|
||||
{
|
||||
std::reference_wrapper<T> r(t);
|
||||
assert(&r.get() == &t);
|
||||
}
|
||||
|
||||
void f() {}
|
||||
|
||||
int main()
|
||||
{
|
||||
void (*fp)() = f;
|
||||
test(fp);
|
||||
test(f);
|
||||
functor1 f1;
|
||||
test(f1);
|
||||
int i = 0;
|
||||
test(i);
|
||||
const int j = 0;
|
||||
test(j);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <ObjectType T> reference_wrapper<const T> cref(const T& t);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 0;
|
||||
std::reference_wrapper<const int> r = std::cref(i);
|
||||
assert(&r.get() == &i);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <ObjectType T> reference_wrapper<const T> cref(reference_wrapper<T> t);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
const int i = 0;
|
||||
std::reference_wrapper<const int> r1 = std::cref(i);
|
||||
std::reference_wrapper<const int> r2 = std::cref(r1);
|
||||
assert(&r2.get() == &i);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <ObjectType T> reference_wrapper<T> ref(T& t);
|
||||
|
||||
// Don't allow binding to a temp
|
||||
|
||||
#include <functional>
|
||||
|
||||
struct A {};
|
||||
|
||||
const A source() {return A();}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::reference_wrapper<const A> r = std::ref(source());
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <ObjectType T> reference_wrapper<T> ref(T& t);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 0;
|
||||
std::reference_wrapper<int> r = std::ref(i);
|
||||
assert(&r.get() == &i);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <ObjectType T> reference_wrapper<T> ref(reference_wrapper<T>t);
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
#include "counting_predicates.hpp"
|
||||
|
||||
bool is5 ( int i ) { return i == 5; }
|
||||
|
||||
template <typename T>
|
||||
bool call_pred ( T pred ) { return pred(5); }
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
int i = 0;
|
||||
std::reference_wrapper<int> r1 = std::ref(i);
|
||||
std::reference_wrapper<int> r2 = std::ref(r1);
|
||||
assert(&r2.get() == &i);
|
||||
}
|
||||
{
|
||||
unary_counting_predicate<bool(*)(int), int> cp(is5);
|
||||
assert(!cp(6));
|
||||
assert(cp.count() == 1);
|
||||
assert(call_pred(cp));
|
||||
assert(cp.count() == 1);
|
||||
assert(call_pred(std::ref(cp)));
|
||||
assert(cp.count() == 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <class... ArgTypes>
|
||||
// requires Callable<T, ArgTypes&&...>
|
||||
// Callable<T, ArgTypes&&...>::result_type
|
||||
// operator()(ArgTypes&&... args) const;
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
// member data pointer: cv qualifiers should transfer from argument to return type
|
||||
|
||||
struct A_int_1
|
||||
{
|
||||
A_int_1() : data_(5) {}
|
||||
|
||||
int data_;
|
||||
};
|
||||
|
||||
void
|
||||
test_int_1()
|
||||
{
|
||||
// member data pointer
|
||||
{
|
||||
int A_int_1::*fp = &A_int_1::data_;
|
||||
std::reference_wrapper<int A_int_1::*> r1(fp);
|
||||
A_int_1 a;
|
||||
assert(r1(a) == 5);
|
||||
r1(a) = 6;
|
||||
assert(r1(a) == 6);
|
||||
const A_int_1* ap = &a;
|
||||
assert(r1(ap) == 6);
|
||||
r1(ap) = 7;
|
||||
assert(r1(ap) == 7);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_int_1();
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <class... ArgTypes>
|
||||
// requires Callable<T, ArgTypes&&...>
|
||||
// Callable<T, ArgTypes&&...>::result_type
|
||||
// operator()(ArgTypes&&... args) const;
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
int count = 0;
|
||||
|
||||
// 1 arg, return void
|
||||
|
||||
void f_void_1(int i)
|
||||
{
|
||||
count += i;
|
||||
}
|
||||
|
||||
struct A_void_1
|
||||
{
|
||||
void operator()(int i)
|
||||
{
|
||||
count += i;
|
||||
}
|
||||
|
||||
void mem1() {++count;}
|
||||
void mem2() const {++count;}
|
||||
};
|
||||
|
||||
void
|
||||
test_void_1()
|
||||
{
|
||||
int save_count = count;
|
||||
// function
|
||||
{
|
||||
std::reference_wrapper<void (int)> r1(f_void_1);
|
||||
int i = 2;
|
||||
r1(i);
|
||||
assert(count == save_count+2);
|
||||
save_count = count;
|
||||
}
|
||||
// function pointer
|
||||
{
|
||||
void (*fp)(int) = f_void_1;
|
||||
std::reference_wrapper<void (*)(int)> r1(fp);
|
||||
int i = 3;
|
||||
r1(i);
|
||||
assert(count == save_count+3);
|
||||
save_count = count;
|
||||
}
|
||||
// functor
|
||||
{
|
||||
A_void_1 a0;
|
||||
std::reference_wrapper<A_void_1> r1(a0);
|
||||
int i = 4;
|
||||
r1(i);
|
||||
assert(count == save_count+4);
|
||||
save_count = count;
|
||||
}
|
||||
// member function pointer
|
||||
{
|
||||
void (A_void_1::*fp)() = &A_void_1::mem1;
|
||||
std::reference_wrapper<void (A_void_1::*)()> r1(fp);
|
||||
A_void_1 a;
|
||||
r1(a);
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
A_void_1* ap = &a;
|
||||
r1(ap);
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
}
|
||||
// const member function pointer
|
||||
{
|
||||
void (A_void_1::*fp)() const = &A_void_1::mem2;
|
||||
std::reference_wrapper<void (A_void_1::*)() const> r1(fp);
|
||||
A_void_1 a;
|
||||
r1(a);
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
A_void_1* ap = &a;
|
||||
r1(ap);
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
}
|
||||
}
|
||||
|
||||
// 1 arg, return int
|
||||
|
||||
int f_int_1(int i)
|
||||
{
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
struct A_int_1
|
||||
{
|
||||
A_int_1() : data_(5) {}
|
||||
int operator()(int i)
|
||||
{
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
int mem1() {return 3;}
|
||||
int mem2() const {return 4;}
|
||||
int data_;
|
||||
};
|
||||
|
||||
void
|
||||
test_int_1()
|
||||
{
|
||||
// function
|
||||
{
|
||||
std::reference_wrapper<int (int)> r1(f_int_1);
|
||||
int i = 2;
|
||||
assert(r1(i) == 3);
|
||||
}
|
||||
// function pointer
|
||||
{
|
||||
int (*fp)(int) = f_int_1;
|
||||
std::reference_wrapper<int (*)(int)> r1(fp);
|
||||
int i = 3;
|
||||
assert(r1(i) == 4);
|
||||
}
|
||||
// functor
|
||||
{
|
||||
A_int_1 a0;
|
||||
std::reference_wrapper<A_int_1> r1(a0);
|
||||
int i = 4;
|
||||
assert(r1(i) == 3);
|
||||
}
|
||||
// member function pointer
|
||||
{
|
||||
int (A_int_1::*fp)() = &A_int_1::mem1;
|
||||
std::reference_wrapper<int (A_int_1::*)()> r1(fp);
|
||||
A_int_1 a;
|
||||
assert(r1(a) == 3);
|
||||
A_int_1* ap = &a;
|
||||
assert(r1(ap) == 3);
|
||||
}
|
||||
// const member function pointer
|
||||
{
|
||||
int (A_int_1::*fp)() const = &A_int_1::mem2;
|
||||
std::reference_wrapper<int (A_int_1::*)() const> r1(fp);
|
||||
A_int_1 a;
|
||||
assert(r1(a) == 4);
|
||||
A_int_1* ap = &a;
|
||||
assert(r1(ap) == 4);
|
||||
}
|
||||
// member data pointer
|
||||
{
|
||||
int A_int_1::*fp = &A_int_1::data_;
|
||||
std::reference_wrapper<int A_int_1::*> r1(fp);
|
||||
A_int_1 a;
|
||||
assert(r1(a) == 5);
|
||||
r1(a) = 6;
|
||||
assert(r1(a) == 6);
|
||||
A_int_1* ap = &a;
|
||||
assert(r1(ap) == 6);
|
||||
r1(ap) = 7;
|
||||
assert(r1(ap) == 7);
|
||||
}
|
||||
}
|
||||
|
||||
// 2 arg, return void
|
||||
|
||||
void f_void_2(int i, int j)
|
||||
{
|
||||
count += i+j;
|
||||
}
|
||||
|
||||
struct A_void_2
|
||||
{
|
||||
void operator()(int i, int j)
|
||||
{
|
||||
count += i+j;
|
||||
}
|
||||
|
||||
void mem1(int i) {count += i;}
|
||||
void mem2(int i) const {count += i;}
|
||||
};
|
||||
|
||||
void
|
||||
test_void_2()
|
||||
{
|
||||
int save_count = count;
|
||||
// function
|
||||
{
|
||||
std::reference_wrapper<void (int, int)> r1(f_void_2);
|
||||
int i = 2;
|
||||
int j = 3;
|
||||
r1(i, j);
|
||||
assert(count == save_count+5);
|
||||
save_count = count;
|
||||
}
|
||||
// function pointer
|
||||
{
|
||||
void (*fp)(int, int) = f_void_2;
|
||||
std::reference_wrapper<void (*)(int, int)> r1(fp);
|
||||
int i = 3;
|
||||
int j = 4;
|
||||
r1(i, j);
|
||||
assert(count == save_count+7);
|
||||
save_count = count;
|
||||
}
|
||||
// functor
|
||||
{
|
||||
A_void_2 a0;
|
||||
std::reference_wrapper<A_void_2> r1(a0);
|
||||
int i = 4;
|
||||
int j = 5;
|
||||
r1(i, j);
|
||||
assert(count == save_count+9);
|
||||
save_count = count;
|
||||
}
|
||||
// member function pointer
|
||||
{
|
||||
void (A_void_2::*fp)(int) = &A_void_2::mem1;
|
||||
std::reference_wrapper<void (A_void_2::*)(int)> r1(fp);
|
||||
A_void_2 a;
|
||||
int i = 3;
|
||||
r1(a, i);
|
||||
assert(count == save_count+3);
|
||||
save_count = count;
|
||||
A_void_2* ap = &a;
|
||||
r1(ap, i);
|
||||
assert(count == save_count+3);
|
||||
save_count = count;
|
||||
}
|
||||
// const member function pointer
|
||||
{
|
||||
void (A_void_2::*fp)(int) const = &A_void_2::mem2;
|
||||
std::reference_wrapper<void (A_void_2::*)(int) const> r1(fp);
|
||||
A_void_2 a;
|
||||
int i = 4;
|
||||
r1(a, i);
|
||||
assert(count == save_count+4);
|
||||
save_count = count;
|
||||
A_void_2* ap = &a;
|
||||
r1(ap, i);
|
||||
assert(count == save_count+4);
|
||||
save_count = count;
|
||||
}
|
||||
}
|
||||
|
||||
// 2 arg, return int
|
||||
|
||||
int f_int_2(int i, int j)
|
||||
{
|
||||
return i+j;
|
||||
}
|
||||
|
||||
struct A_int_2
|
||||
{
|
||||
int operator()(int i, int j)
|
||||
{
|
||||
return i+j;
|
||||
}
|
||||
|
||||
int mem1(int i) {return i+1;}
|
||||
int mem2(int i) const {return i+2;}
|
||||
};
|
||||
|
||||
void
|
||||
testint_2()
|
||||
{
|
||||
// function
|
||||
{
|
||||
std::reference_wrapper<int (int, int)> r1(f_int_2);
|
||||
int i = 2;
|
||||
int j = 3;
|
||||
assert(r1(i, j) == i+j);
|
||||
}
|
||||
// function pointer
|
||||
{
|
||||
int (*fp)(int, int) = f_int_2;
|
||||
std::reference_wrapper<int (*)(int, int)> r1(fp);
|
||||
int i = 3;
|
||||
int j = 4;
|
||||
assert(r1(i, j) == i+j);
|
||||
}
|
||||
// functor
|
||||
{
|
||||
A_int_2 a0;
|
||||
std::reference_wrapper<A_int_2> r1(a0);
|
||||
int i = 4;
|
||||
int j = 5;
|
||||
assert(r1(i, j) == i+j);
|
||||
}
|
||||
// member function pointer
|
||||
{
|
||||
int(A_int_2::*fp)(int) = &A_int_2::mem1;
|
||||
std::reference_wrapper<int (A_int_2::*)(int)> r1(fp);
|
||||
A_int_2 a;
|
||||
int i = 3;
|
||||
assert(r1(a, i) == i+1);
|
||||
A_int_2* ap = &a;
|
||||
assert(r1(ap, i) == i+1);
|
||||
}
|
||||
// const member function pointer
|
||||
{
|
||||
int (A_int_2::*fp)(int) const = &A_int_2::mem2;
|
||||
std::reference_wrapper<int (A_int_2::*)(int) const> r1(fp);
|
||||
A_int_2 a;
|
||||
int i = 4;
|
||||
assert(r1(a, i) == i+2);
|
||||
A_int_2* ap = &a;
|
||||
assert(r1(ap, i) == i+2);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_void_1();
|
||||
test_int_1();
|
||||
test_void_2();
|
||||
testint_2();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <class... ArgTypes>
|
||||
// requires Callable<T, ArgTypes&&...>
|
||||
// Callable<T, ArgTypes&&...>::result_type
|
||||
// operator()(ArgTypes&&... args) const;
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
// 0 args, return int
|
||||
|
||||
int count = 0;
|
||||
|
||||
int f_int_0()
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
struct A_int_0
|
||||
{
|
||||
int operator()() {return 4;}
|
||||
};
|
||||
|
||||
void
|
||||
test_int_0()
|
||||
{
|
||||
// function
|
||||
{
|
||||
std::reference_wrapper<int ()> r1(f_int_0);
|
||||
assert(r1() == 3);
|
||||
}
|
||||
// function pointer
|
||||
{
|
||||
int (*fp)() = f_int_0;
|
||||
std::reference_wrapper<int (*)()> r1(fp);
|
||||
assert(r1() == 3);
|
||||
}
|
||||
// functor
|
||||
{
|
||||
A_int_0 a0;
|
||||
std::reference_wrapper<A_int_0> r1(a0);
|
||||
assert(r1() == 4);
|
||||
}
|
||||
}
|
||||
|
||||
// 1 arg, return void
|
||||
|
||||
void f_void_1(int i)
|
||||
{
|
||||
count += i;
|
||||
}
|
||||
|
||||
struct A_void_1
|
||||
{
|
||||
void operator()(int i)
|
||||
{
|
||||
count += i;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
test_int_0();
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// template <class... ArgTypes>
|
||||
// requires Callable<T, ArgTypes&&...>
|
||||
// Callable<T, ArgTypes&&...>::result_type
|
||||
// operator()(ArgTypes&&... args) const;
|
||||
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
// 0 args, return void
|
||||
|
||||
int count = 0;
|
||||
|
||||
void f_void_0()
|
||||
{
|
||||
++count;
|
||||
}
|
||||
|
||||
struct A_void_0
|
||||
{
|
||||
void operator()() {++count;}
|
||||
};
|
||||
|
||||
void
|
||||
test_void_0()
|
||||
{
|
||||
int save_count = count;
|
||||
// function
|
||||
{
|
||||
std::reference_wrapper<void ()> r1(f_void_0);
|
||||
r1();
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
}
|
||||
// function pointer
|
||||
{
|
||||
void (*fp)() = f_void_0;
|
||||
std::reference_wrapper<void (*)()> r1(fp);
|
||||
r1();
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
}
|
||||
// functor
|
||||
{
|
||||
A_void_0 a0;
|
||||
std::reference_wrapper<A_void_0> r1(a0);
|
||||
r1();
|
||||
assert(count == save_count+1);
|
||||
save_count = count;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_void_0();
|
||||
}
|
||||
37
test/std/utilities/function.objects/refwrap/type.pass.cpp
Normal file
37
test/std/utilities/function.objects/refwrap/type.pass.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// check for member typedef type
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
class C {};
|
||||
|
||||
int main()
|
||||
{
|
||||
static_assert((std::is_same<std::reference_wrapper<C>::type,
|
||||
C>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<void ()>::type,
|
||||
void ()>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int* (double*)>::type,
|
||||
int* (double*)>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<void(*)()>::type,
|
||||
void(*)()>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int*(*)(double*)>::type,
|
||||
int*(*)(double*)>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int*(C::*)(double*)>::type,
|
||||
int*(C::*)(double*)>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int (C::*)(double*) const volatile>::type,
|
||||
int (C::*)(double*) const volatile>::value), "");
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// Test that reference wrapper meets the requirements of TriviallyCopyable,
|
||||
// CopyConstructible and CopyAssignable.
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <string>
|
||||
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
class MoveOnly
|
||||
{
|
||||
MoveOnly(const MoveOnly&);
|
||||
MoveOnly& operator=(const MoveOnly&);
|
||||
|
||||
int data_;
|
||||
public:
|
||||
MoveOnly(int data = 1) : data_(data) {}
|
||||
MoveOnly(MoveOnly&& x)
|
||||
: data_(x.data_) {x.data_ = 0;}
|
||||
MoveOnly& operator=(MoveOnly&& x)
|
||||
{data_ = x.data_; x.data_ = 0; return *this;}
|
||||
|
||||
int get() const {return data_;}
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
template <class T>
|
||||
void test()
|
||||
{
|
||||
typedef std::reference_wrapper<T> Wrap;
|
||||
static_assert(std::is_copy_constructible<Wrap>::value, "");
|
||||
static_assert(std::is_copy_assignable<Wrap>::value, "");
|
||||
// Extension up for standardization: See N4151.
|
||||
static_assert(std::is_trivially_copyable<Wrap>::value, "");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test<int>();
|
||||
test<double>();
|
||||
test<std::string>();
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
test<MoveOnly>();
|
||||
#endif
|
||||
}
|
||||
78
test/std/utilities/function.objects/refwrap/unary.pass.cpp
Normal file
78
test/std/utilities/function.objects/refwrap/unary.pass.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// check for deriving from unary_function
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
class functor2
|
||||
: public std::binary_function<char, int, double>
|
||||
{
|
||||
};
|
||||
|
||||
class functor3
|
||||
: public std::unary_function<int, int>,
|
||||
public std::binary_function<char, int, double>
|
||||
{
|
||||
public:
|
||||
typedef float result_type;
|
||||
};
|
||||
|
||||
class functor4
|
||||
: public std::unary_function<int, int>,
|
||||
public std::binary_function<char, int, double>
|
||||
{
|
||||
public:
|
||||
};
|
||||
|
||||
struct C
|
||||
{
|
||||
typedef int argument_type;
|
||||
typedef int result_type;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
static_assert((std::is_base_of<std::unary_function<int, char>,
|
||||
std::reference_wrapper<functor1> >::value), "");
|
||||
static_assert((!std::is_base_of<std::unary_function<char, int>,
|
||||
std::reference_wrapper<functor2> >::value), "");
|
||||
static_assert((std::is_base_of<std::unary_function<int, int>,
|
||||
std::reference_wrapper<functor3> >::value), "");
|
||||
static_assert((std::is_base_of<std::unary_function<int, int>,
|
||||
std::reference_wrapper<functor4> >::value), "");
|
||||
static_assert((!std::is_base_of<std::unary_function<int, int>,
|
||||
std::reference_wrapper<C> >::value), "");
|
||||
static_assert((!std::is_base_of<std::unary_function<int, float>,
|
||||
std::reference_wrapper<float(*)()> >::value), "");
|
||||
static_assert((std::is_base_of<std::unary_function<int, float>,
|
||||
std::reference_wrapper<float (int)> >::value), "");
|
||||
static_assert((!std::is_base_of<std::unary_function<int, float>,
|
||||
std::reference_wrapper<float (int, int)> >::value), "");
|
||||
static_assert((std::is_base_of<std::unary_function<int, float>,
|
||||
std::reference_wrapper<float(*)(int)> >::value), "");
|
||||
static_assert((!std::is_base_of<std::unary_function<int, float>,
|
||||
std::reference_wrapper<float(*)(int, int)> >::value), "");
|
||||
static_assert((std::is_base_of<std::unary_function<C*, float>,
|
||||
std::reference_wrapper<float(C::*)()> >::value), "");
|
||||
static_assert((std::is_base_of<std::unary_function<const volatile C*, float>,
|
||||
std::reference_wrapper<float(C::*)() const volatile> >::value), "");
|
||||
static_assert((!std::is_base_of<std::unary_function<C*, float>,
|
||||
std::reference_wrapper<float(C::*)(int)> >::value), "");
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <functional>
|
||||
|
||||
// reference_wrapper
|
||||
|
||||
// has weak result type
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
class functor1
|
||||
: public std::unary_function<int, char>
|
||||
{
|
||||
};
|
||||
|
||||
class functor2
|
||||
: public std::binary_function<char, int, double>
|
||||
{
|
||||
};
|
||||
|
||||
class functor3
|
||||
: public std::unary_function<char, int>,
|
||||
public std::binary_function<char, int, double>
|
||||
{
|
||||
public:
|
||||
typedef float result_type;
|
||||
};
|
||||
|
||||
class functor4
|
||||
: public std::unary_function<char, int>,
|
||||
public std::binary_function<char, int, double>
|
||||
{
|
||||
public:
|
||||
};
|
||||
|
||||
class C {};
|
||||
|
||||
template <class T>
|
||||
struct has_result_type
|
||||
{
|
||||
private:
|
||||
struct two {char _; char __;};
|
||||
template <class U> static two test(...);
|
||||
template <class U> static char test(typename U::result_type* = 0);
|
||||
public:
|
||||
static const bool value = sizeof(test<T>(0)) == 1;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
static_assert((std::is_same<std::reference_wrapper<functor1>::result_type,
|
||||
char>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<functor2>::result_type,
|
||||
double>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<functor3>::result_type,
|
||||
float>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<void()>::result_type,
|
||||
void>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int*(double*)>::result_type,
|
||||
int*>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<void(*)()>::result_type,
|
||||
void>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int*(*)(double*)>::result_type,
|
||||
int*>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int*(C::*)(double*)>::result_type,
|
||||
int*>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<int (C::*)(double*) const volatile>::result_type,
|
||||
int>::value), "");
|
||||
static_assert((std::is_same<std::reference_wrapper<C()>::result_type,
|
||||
C>::value), "");
|
||||
static_assert(has_result_type<std::reference_wrapper<functor3> >::value, "");
|
||||
static_assert(!has_result_type<std::reference_wrapper<functor4> >::value, "");
|
||||
static_assert(!has_result_type<std::reference_wrapper<C> >::value, "");
|
||||
}
|
||||
Reference in New Issue
Block a user