Added func value_or_eval()

This commit is contained in:
Andrzej Krzemienski
2014-06-18 15:01:52 +02:00
parent a158b85bd6
commit d70114b3dc
14 changed files with 536 additions and 50 deletions

View File

@ -42,5 +42,6 @@ import testing ;
[ compile-fail optional_test_ref_fail_assign_from_Trefref.cpp ]
[ compile-fail optional_test_ref_fail_assign_from_Urefref.cpp ]
[ compile-fail optional_test_fail_explicit_convert_in_value_or.cpp ]
[ compile-fail optional_test_fail_explicit_convert_in_value_or_call.cpp ]
;
}

View File

@ -0,0 +1,34 @@
// Copyright (C) 2014, Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// akrzemi1@gmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
struct U
{};
struct T
{
explicit T(U const&) {}
};
U get_U() { return U(); }
void test_verifying_the_implicit_conversion_to_bool()
{
boost::optional<T> opt;
opt.value_or_eval(get_U);
}

View File

@ -128,6 +128,73 @@ void test_function_value_or()
BOOST_CHECK(FatToIntConverter::conversions == 1);
}
struct FunM
{
int operator()() { return 5; }
};
struct FunC
{
int operator()() const { return 6; }
};
int funP ()
{
return 7;
}
int throw_()
{
throw int();
}
void test_function_value_or_call()
{
optional<int> o1 = 1;
optional<int> oN;
FunM funM;
FunC funC;
BOOST_CHECK(o1.value_or_eval(funM) == 1);
BOOST_CHECK(oN.value_or_eval(funM) == 5);
BOOST_CHECK(o1.value_or_eval(FunM()) == 1);
BOOST_CHECK(oN.value_or_eval(FunM()) == 5);
BOOST_CHECK(o1.value_or_eval(funC) == 1);
BOOST_CHECK(oN.value_or_eval(funC) == 6);
BOOST_CHECK(o1.value_or_eval(FunC()) == 1);
BOOST_CHECK(oN.value_or_eval(FunC()) == 6);
BOOST_CHECK(o1.value_or_eval(funP) == 1);
BOOST_CHECK(oN.value_or_eval(funP) == 7);
#ifndef BOOST_NO_CXX11_LAMBDAS
BOOST_CHECK(o1.value_or_eval([](){return 8;}) == 1);
BOOST_CHECK(oN.value_or_eval([](){return 8;}) == 8);
#endif
try
{
BOOST_CHECK(o1.value_or_eval(throw_) == 1);
}
catch(...)
{
BOOST_CHECK(false);
}
try
{
BOOST_CHECK(oN.value_or_eval(throw_) == 1);
BOOST_CHECK(false);
}
catch(...)
{
BOOST_CHECK(true);
}
}
#ifndef BOOST_NO_CXX11_REF_QUALIFIERS
struct MoveOnly
{
@ -144,15 +211,22 @@ optional<MoveOnly> makeMoveOnly()
return MoveOnly(1);
}
MoveOnly moveOnlyDefault()
{
return MoveOnly(1);
}
// compile-time test
void test_move_only_getters()
{
MoveOnly m1 = *makeMoveOnly();
MoveOnly m2 = makeMoveOnly().value();
MoveOnly m3 = makeMoveOnly().value_or(MoveOnly(1));
MoveOnly m4 = makeMoveOnly().value_or_eval(moveOnlyDefault);
unused_variable(m1);
unused_variable(m2);
unused_variable(m3);
unused_variable(m4);
}
#endif
@ -163,6 +237,7 @@ int test_main( int, char* [] )
{
test_function_value();
test_function_value_or();
test_function_value_or_call();
}
catch ( ... )
{