Compare commits

..

7 Commits

Author SHA1 Message Date
Peter Dimov
1da3892239 Disable C4702 (unreachable code) for the throwing tests 2019-03-25 19:50:33 +02:00
Peter Dimov
3808c9b122 Fix variant_npos warning 2019-03-25 17:40:39 +02:00
Peter Dimov
f1b1e7cade Enable warnings-as-errors in test/Jamfile 2019-03-25 17:40:20 +02:00
Peter Dimov
9875cfdc98 Add variant_npos and valueless_by_exception() 2019-03-25 16:17:06 +02:00
Peter Dimov
688849f9e8 Remove outcome.hpp and result.hpp 2019-03-25 16:16:10 +02:00
Peter Dimov
1e986047ae Merge pull request #6 from akrzemi1/patch-1
docs: sizeof(T...) -> sizeof...(T)
2019-03-06 19:09:31 +02:00
Andrzej Krzemieński
107a43d5a0 docs: sizeof(T...) -> sizeof...(T) 2019-03-06 16:07:56 +01:00
12 changed files with 54 additions and 490 deletions

View File

@@ -496,7 +496,7 @@ template<size_t I, class... A>
[none]
* {blank}
+
Requires: :: `I < sizeof(T...)`.
Requires: :: `I < sizeof...(T)`.
Effects: ::
Destroys the currently contained value, then initializes a new contained
value as if using the expression `Ti(std::forward<A>(a)...)`.
@@ -514,7 +514,7 @@ template<size_t I, class V, class... A>
[none]
* {blank}
+
Requires: :: `I < sizeof(T...)`.
Requires: :: `I < sizeof...(T)`.
Effects: ::
Destroys the currently contained value, then initializes a new contained
value as if using the expression `Ti(il, std::forward<A>(a)...)`.

View File

@@ -1,259 +0,0 @@
#ifndef BOOST_VARIANT2_OUTCOME_HPP_INCLUDED
#define BOOST_VARIANT2_OUTCOME_HPP_INCLUDED
// Copyright 2017 Peter Dimov.
//
// Distributed under 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
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
#include <boost/variant2/variant.hpp>
#endif
#include <system_error>
#include <exception>
#include <type_traits>
#include <cassert>
//
namespace boost
{
namespace variant2
{
enum class outcome_errc
{
not_initialized
};
class outcome_error_category: public std::error_category
{
public:
virtual const char * name() const noexcept
{
return "boost::variant2::outcome";
}
virtual std::string message( int e ) const
{
switch( e )
{
case (int)outcome_errc::not_initialized:
return "outcome<> not initialized";
default:
return "unknown outcome<> error";
}
}
static outcome_error_category const & instance()
{
static outcome_error_category cat;
return cat;
}
};
std::error_code make_error_code( outcome_errc e )
{
return std::error_code( static_cast<int>( e ), outcome_error_category::instance() );
}
template<class T> class outcome
{
private:
variant<T, std::error_code, std::exception_ptr> v_;
public:
// constructors
constexpr outcome() noexcept: v_( make_error_code( outcome_errc::not_initialized ) )
{
}
constexpr outcome( T const& t ): v_( t )
{
}
constexpr outcome( T&& t ): v_( std::move(t) )
{
}
constexpr outcome( std::error_code const & ec ) noexcept: v_( ec )
{
}
constexpr outcome( std::exception_ptr const & ep ) noexcept: v_( ep )
{
}
// queries
constexpr bool has_value() const noexcept
{
return v_.index() == 0;
}
constexpr bool has_error() const noexcept
{
return v_.index() == 1;
}
constexpr bool has_exception() const noexcept
{
return v_.index() == 2;
}
constexpr explicit operator bool() const noexcept
{
return v_.index() == 0;
}
// checked value access
constexpr T& value() &
{
if( has_value() )
{
return *get_if<0>(&v_);
}
else
{
throw std::system_error( *get_if<1>(&v_) );
}
}
constexpr T const& value() const&
{
if( has_value() )
{
return *get_if<0>(&v_);
}
else
{
throw std::system_error( *get_if<1>(&v_) );
}
}
constexpr T&& value() &&
{
return std::move( value() );
}
constexpr T const&& value() const&&
{
return std::move( value() );
}
// unchecked value access
T* operator->() noexcept
{
return get_if<0>(&v_);
}
T const* operator->() const noexcept
{
return get_if<0>(&v_);
}
T& operator*() & noexcept
{
T* p = get_if<0>(&v_);
assert( p != 0 );
return *p;
}
T const& operator*() const & noexcept
{
T const* p = get_if<0>(&v_);
assert( p != 0 );
return *p;
}
T&& operator*() && noexcept
{
return std::move(**this);
}
T const&& operator*() const && noexcept
{
return std::move(**this);
}
// error access
/*constexpr*/ std::error_code error() const noexcept
{
if( has_error() )
{
return *get_if<1>(&v_);
}
else
{
return std::error_code();
}
}
// exception access
std::exception_ptr exception() const noexcept
{
if( has_exception() )
{
return *get_if<2>(&v_);
}
else if( has_value() )
{
return std::exception_ptr();
}
else
{
return std::make_exception_ptr( std::system_error( *get_if<1>(&v_) ) );
}
}
// setters
void set_value( T const& t ) noexcept( std::is_nothrow_copy_constructible<T>::value )
{
v_.emplace<0>( t );
}
void set_value( T&& t ) noexcept( std::is_nothrow_move_constructible<T>::value )
{
v_.emplace<0>( std::move( t ) );
}
void set_error( std::error_code const & e ) noexcept
{
v_.emplace<1>( e );
}
void set_exception( std::exception_ptr const & x ) noexcept
{
v_.emplace<2>( x );
}
// swap
void swap( outcome& r ) noexcept( noexcept( v_.swap( r.v_ ) ) )
{
v_.swap( r.v_ );
}
};
} // namespace variant2
} // namespace boost
#endif // #ifndef BOOST_VARIANT2_OUTCOME_HPP_INCLUDED

View File

@@ -1,226 +0,0 @@
#ifndef BOOST_VARIANT2_RESULT_HPP_INCLUDED
#define BOOST_VARIANT2_RESULT_HPP_INCLUDED
// Copyright 2017 Peter Dimov.
//
// Distributed under 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
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
#include <boost/variant2/variant.hpp>
#endif
#include <system_error>
#include <type_traits>
#include <cassert>
//
namespace boost
{
namespace variant2
{
enum class result_errc
{
not_initialized
};
class result_error_category: public std::error_category
{
public:
virtual const char * name() const noexcept
{
return "boost::variant2::result";
}
virtual std::string message( int e ) const
{
switch( e )
{
case (int)result_errc::not_initialized:
return "result<> not initialized";
default:
return "unknown result<> error";
}
}
static result_error_category const & instance()
{
static result_error_category cat;
return cat;
}
};
std::error_code make_error_code( result_errc e )
{
return std::error_code( static_cast<int>( e ), result_error_category::instance() );
}
template<class T> class result
{
private:
variant<T, std::error_code> v_;
public:
// constructors
constexpr result() noexcept: v_( make_error_code( result_errc::not_initialized ) )
{
}
constexpr result( T const& t ): v_( t )
{
}
constexpr result( T&& t ): v_( std::move(t) )
{
}
constexpr result( std::error_code const & ec ) noexcept: v_( ec )
{
}
// queries
constexpr bool has_value() const noexcept
{
return v_.index() == 0;
}
constexpr bool has_error() const noexcept
{
return v_.index() == 1;
}
constexpr explicit operator bool() const noexcept
{
return v_.index() == 0;
}
// checked value access
constexpr T& value() &
{
if( has_value() )
{
return *get_if<0>(&v_);
}
else
{
throw std::system_error( *get_if<1>(&v_) );
}
}
constexpr T const& value() const&
{
if( has_value() )
{
return *get_if<0>(&v_);
}
else
{
throw std::system_error( *get_if<1>(&v_) );
}
}
constexpr T&& value() &&
{
return std::move( value() );
}
constexpr T const&& value() const&&
{
return std::move( value() );
}
// unchecked value access
T* operator->() noexcept
{
return get_if<0>(&v_);
}
T const* operator->() const noexcept
{
return get_if<0>(&v_);
}
T& operator*() & noexcept
{
T* p = get_if<0>(&v_);
assert( p != 0 );
return *p;
}
T const& operator*() const & noexcept
{
T const* p = get_if<0>(&v_);
assert( p != 0 );
return *p;
}
T&& operator*() && noexcept
{
return std::move(**this);
}
T const&& operator*() const && noexcept
{
return std::move(**this);
}
// error access
/*constexpr*/ std::error_code error() const noexcept
{
if( has_error() )
{
return *get_if<1>(&v_);
}
else
{
return std::error_code();
}
}
// setters
void set_value( T const& t ) noexcept( std::is_nothrow_copy_constructible<T>::value )
{
v_.emplace<0>( t );
}
void set_value( T&& t ) noexcept( std::is_nothrow_move_constructible<T>::value )
{
v_.emplace<0>( std::move( t ) );
}
void set_error( std::error_code const & e ) noexcept
{
v_.emplace<1>( e );
}
// swap
void swap( result& r ) noexcept( noexcept( v_.swap( r.v_ ) ) )
{
v_.swap( r.v_ );
}
};
} // namespace variant2
} // namespace boost
#endif // #ifndef BOOST_VARIANT2_RESULT_HPP_INCLUDED

View File

@@ -1,7 +1,7 @@
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
#define BOOST_VARIANT2_VARIANT_HPP_INCLUDED
// Copyright 2017, 2018 Peter Dimov.
// Copyright 2017-2019 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
@@ -266,6 +266,10 @@ template<std::size_t I, class... T> struct variant_alternative<I, variant<T...>>
#endif
// variant_npos
constexpr std::size_t variant_npos = ~static_cast<std::size_t>( 0 );
// holds_alternative
template<class U, class... T> constexpr bool holds_alternative( variant<T...> const& v ) noexcept
@@ -1350,6 +1354,11 @@ public:
// value status
constexpr bool valueless_by_exception() const noexcept
{
return false;
}
using variant_base::index;
// swap

View File

@@ -1,6 +1,6 @@
# Boost.Variant2 Library Test Jamfile
#
# Copyright 2015-2017 Peter Dimov
# Copyright 2015-2019 Peter Dimov
#
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or copy at
@@ -9,7 +9,19 @@
import testing ;
import ../../config/checks/config : requires ;
project : requirements [ requires cxx11_variadic_templates cxx11_template_aliases cxx11_decltype cxx11_constexpr ] ;
project
: default-build
<warnings>all
: requirements
[ requires cxx11_variadic_templates cxx11_template_aliases cxx11_decltype cxx11_constexpr ]
<toolset>msvc:<warnings-as-errors>on
<toolset>gcc:<warnings-as-errors>on
<toolset>clang:<warnings-as-errors>on
;
run variant_size.cpp ;
run variant_alternative.cpp ;

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <stdexcept>

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <stdexcept>

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <stdexcept>

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <stdexcept>

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <stdexcept>

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>

View File

@@ -6,6 +6,10 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4702 ) // unreachable code
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <type_traits>