omit unnecessary bits when explicit operator conversions are supported

This commit is contained in:
James E. King III
2018-10-13 15:40:17 +00:00
parent d76d7dc4d2
commit 82a233bee1
10 changed files with 197 additions and 7 deletions

View File

@ -71,6 +71,7 @@ indeterminate(tribool x,
*/ */
class tribool class tribool
{ {
#if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS )
private: private:
/// INTERNAL ONLY /// INTERNAL ONLY
struct dummy { struct dummy {
@ -78,6 +79,7 @@ private:
}; };
typedef void (dummy::*safe_bool)(); typedef void (dummy::*safe_bool)();
#endif
public: public:
/** /**

View File

@ -8,10 +8,55 @@
# For more information, see http://www.boost.org/ # For more information, see http://www.boost.org/
import path ;
import os ;
import regex ;
import testing ;
local self = logic ;
rule test-expected-failures
{
local all_rules = ;
local file ;
local tests_path = [ path.make $(BOOST_ROOT)/libs/$(self)/test/compile-fail ] ;
for file in [ path.glob-tree $(tests_path) : *.cpp ]
{
local rel_file = [ path.relative-to $(tests_path) $(file) ] ;
local test_name = [ regex.replace [ regex.replace $(rel_file) "/" "-" ] ".cpp" "" ] ;
local decl_test_name = cf-$(test_name) ;
# ECHO $(rel_file) ;
all_rules += [ compile-fail $(file) : : $(decl_test_name) ] ;
}
# ECHO All rules: $(all_rules) ;
return $(all_rules) ;
}
rule test-header-isolation
{
local all_rules = ;
local file ;
local headers_path = [ path.make $(BOOST_ROOT)/libs/$(self)/include ] ;
for file in [ path.glob-tree $(headers_path) : *.hpp ]
{
local rel_file = [ path.relative-to $(headers_path) $(file) ] ;
# Note: The test name starts with '~' in order to group these tests in the test report table, preferably at the end.
# All '/' are replaced with '-' because apparently test scripts have a problem with test names containing slashes.
local test_name = [ regex.replace $(rel_file) "/" "-" ] ;
local decl_test_name = ~hdr-decl-$(test_name) ;
# ECHO $(rel_file) ;
all_rules += [ compile compile/decl_header.cpp : <define>"BOOST_TEST_HEADER=$(rel_file)" <dependency>$(file) : $(decl_test_name) ] ;
}
# ECHO All rules: $(all_rules) ;
return $(all_rules) ;
}
test-suite logic : test-suite logic :
[ test-expected-failures ]
[ test-header-isolation ]
[ run tribool_test.cpp ] [ run tribool_test.cpp ]
[ run tribool_rename_test.cpp ] [ run tribool_rename_test.cpp ]
[ run tribool_io_test.cpp ] [ run tribool_io_test.cpp ]
; ;

View File

@ -0,0 +1,23 @@
// Copyright 2018 James E. King III. 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)
#include <boost/logic/tribool.hpp>
int main(int, char*[])
{
using boost::logic::indeterminate;
using boost::logic::tribool;
tribool i(indeterminate);
#if !defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS )
bool b = i; // expect to see: no viable conversion from 'boost::logic::tribool' to 'bool'
return (int)b;
#else
#error in c++03 explicit conversions are allowed
#endif
// NOTREACHED
return 0;
}

View File

@ -0,0 +1,16 @@
// Copyright 2018 James E. King III. 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)
#include <boost/logic/tribool.hpp>
int main(int, char*[])
{
using boost::logic::indeterminate;
using boost::logic::tribool;
tribool i(indeterminate);
return i; // expect to see: error: no viable conversion from returned value of type 'boost::logic::tribool' to function return type 'int'
}

View File

@ -0,0 +1,16 @@
// Copyright 2018 James E. King III. 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)
#include <boost/logic/tribool.hpp>
int main(int, char*[])
{
using boost::logic::indeterminate;
using boost::logic::tribool;
tribool i(indeterminate);
return i + 1; // expect to see: error: invalid operands to binary expression ('boost::logic::tribool' and 'int')
}

View File

@ -0,0 +1,16 @@
// Copyright 2018 James E. King III. 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)
#include <boost/logic/tribool.hpp>
int main(int, char*[])
{
using boost::logic::indeterminate;
using boost::logic::tribool;
tribool i(indeterminate);
return i << 1; // expect to see: error: invalid operands to binary expression ('boost::logic::tribool' and 'int')
}

View File

@ -0,0 +1,16 @@
// Copyright 2018 James E. King III. 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)
#include <boost/logic/tribool.hpp>
int main(int, char*[])
{
using boost::logic::tribool;
tribool f; // false
tribool t(true); // true
return f < t; // expect to see: error: invalid operands to binary expression ('boost::logic::tribool' and 'boost::logic::tribool')
}

View File

@ -0,0 +1,15 @@
// Copyright 2018 James E. King III. 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)
#include <boost/logic/tribool.hpp>
int main(int, char*[])
{
using boost::logic::tribool;
tribool t(true);
return t < 1; // expect to see: error: invalid operands to binary expression ('boost::logic::tribool' and 'int')
}

View File

@ -0,0 +1,24 @@
/*
* Copyright Andrey Semashev 2015.
* 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)
*/
/*!
* \file decl_header.cpp
* \author Andrey Semashev
* \date 21.06.2015
*
* \brief This file contains a test boilerplate for checking that every
* public header is self-contained and does not have any missing
* #includes.
*/
#define BOOST_TEST_INCLUDE_HEADER() <BOOST_TEST_HEADER>
#include BOOST_TEST_INCLUDE_HEADER()
int main(int, char*[])
{
return 0;
}

View File

@ -3,7 +3,7 @@
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
#include <boost/core/ignore_unused.hpp> #include <boost/config.hpp>
#include <boost/logic/tribool.hpp> #include <boost/logic/tribool.hpp>
#include <boost/test/minimal.hpp> #include <boost/test/minimal.hpp>
#include <iostream> #include <iostream>
@ -16,6 +16,19 @@ int test_main(int, char*[])
tribool y(true); // true tribool y(true); // true
tribool z(indeterminate); // indeterminate tribool z(indeterminate); // indeterminate
#if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS )
// c++03 allows for implicit conversion to bool
// c++11 uses an explicit conversion operator so this would not compile
// and that is tested in the compile-fail/implicit.cpp file
// so we check the conversion to ensure it is sane
bool bx = x;
BOOST_CHECK(bx == false);
bool by = y;
BOOST_CHECK(by == true);
bool bz = z;
BOOST_CHECK(bz == false);
#endif
BOOST_CHECK(!x); BOOST_CHECK(!x);
BOOST_CHECK(x == false); BOOST_CHECK(x == false);
BOOST_CHECK(false == x); BOOST_CHECK(false == x);
@ -115,7 +128,7 @@ int test_main(int, char*[])
BOOST_CHECK(false); BOOST_CHECK(false);
} }
#ifndef BOOST_NO_CXX11_CONSTEXPR #if !defined(BOOST_NO_CXX11_CONSTEXPR)
constexpr bool res_ors = indeterminate(false || tribool(false) || false || indeterminate); // true constexpr bool res_ors = indeterminate(false || tribool(false) || false || indeterminate); // true
BOOST_CHECK(res_ors); BOOST_CHECK(res_ors);
char array_ors[res_ors ? 2 : 3]; char array_ors[res_ors ? 2 : 3];
@ -127,9 +140,13 @@ int test_main(int, char*[])
BOOST_CHECK(sizeof(array_ands) / sizeof(char) == 3); BOOST_CHECK(sizeof(array_ands) / sizeof(char) == 3);
constexpr bool res_safe_bool = static_cast<bool>( tribool(true) ); constexpr bool res_safe_bool = static_cast<bool>( tribool(true) );
boost::ignore_unused(res_safe_bool); BOOST_STATIC_ASSERT(res_safe_bool);
constexpr tribool xxx = (tribool(true) || tribool(indeterminate));
static_assert(xxx, "Must be true!"); // gcc 4.6 chokes on the xxx assignment
# if !BOOST_WORKAROUND(BOOST_GCC, < 40700)
constexpr tribool xxx = (tribool(true) || tribool(indeterminate));
BOOST_STATIC_ASSERT(xxx);
# endif
#endif #endif
std::cout << "no errors detected\n"; std::cout << "no errors detected\n";