summaryrefslogtreecommitdiff
path: root/libs/optional/test/optional_test_inplace.cpp
blob: b0e828507c3f9897d6ffa060092703c160f9fcf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// 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:
//  fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string>

#define BOOST_ENABLE_ASSERT_HANDLER

#include "boost/optional/optional.hpp"

#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/utility/in_place_factory.hpp"
#include "boost/utility/typed_in_place_factory.hpp"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "boost/test/minimal.hpp"

#include "optional_test_common.cpp"

#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
struct A
{
  A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {}

  friend bool operator == ( A const& x, A const& y )
    { return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; }

  double      m_a0 ;
  std::string m_a1 ;
} ;

int test_main( int, char* [] )
{
  double a00 = 3.14, a10 = 6.02e-23;
  std::string a01("pi"), a11("mol");

  A a0(a00,a01);
  A a1(a10,a11);

  boost::optional<A> opt1(a0);

  boost::optional<A> opt2 ( boost::in_place(a00,a01) ) ;

  boost::optional<A> opt3 ( boost::in_place<A>(a00,a01) ) ;

  BOOST_CHECK( opt1 == opt2 ) ;
  BOOST_CHECK( opt2 == opt2 ) ;
  BOOST_CHECK( *opt2 == a0 ) ;

#ifndef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION

  opt2 = boost::in_place(a10,a11);
  BOOST_CHECK( *opt2 == a1 ) ;

  opt3 = boost::in_place<A>(a10,a11);
  BOOST_CHECK( *opt3 == a1 ) ;

#endif

  return 0;
}
#else
int test_main( int, char* [] )
{
  // If in-place factories are not supported there is nothing to test
  return 0 ;
}
#endif