Boost logo

Boost :

From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2002-05-20 13:32:45


I've tried to use static_assert.hpp with most confusing results.

example_3 seems to fail to compile.

  BOOST_STATIC_ASSERT((std::numeric_limits<UnsignedInt>::is_specialized
                        && std::numeric_limits<UnsignedInt>::is_integer
                        && !std::numeric_limits<UnsignedInt>::is_signed));

and yet some individual parts compile (and thus assert correctly)

Some additions from my attempts to understand the problem.

Suggestions?

Paul

Dr Paul A Bristow, hetp Chromatography
Prizet Farmhouse
Kendal, Cumbria
LA8 8AB UK
+44 1539 561830
Mobile +44 7714 33 02 04
mailto:pbristow_at_[hidden]

// (C) Copyright John Maddock 2000.
// Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice
appears
// in all copies. This software is provided "as is" without express or
implied
// warranty, and with no claim as to its suitability for any purpose.

// See http://www.boost.org for most recent version including
documentation.

//#include <limits>
#include <boost/limits.hpp>
#include <boost/static_assert.hpp>

#include <iostream>

using std::cout;
using std::endl;
using std::boolalpha;

// #define BOOST_USE_ENUM_STATIC_ASSERT doesn't help
// /Za allow extensions or not makes no difference.

// #undef BOOST_MSVC makes no difference.

namespace my_conditions // This doesn't make any difference?
{
template <class UnsignedInt>
class myclass
{
private:

   BOOST_STATIC_ASSERT(sizeof(UnsignedInt) * CHAR_BIT >= 16); // OK
  BOOST_STATIC_ASSERT(!std::numeric_limits<UnsignedInt>::is_signed); // OK
  BOOST_STATIC_ASSERT(!std::numeric_limits<UnsignedInt>::is_signed); //
duplicate OK
  BOOST_STATIC_ASSERT(std::numeric_limits<unsigned int>::is_integer); // OK
  BOOST_STATIC_ASSERT(std::numeric_limits<unsigned int>::is_specialized); //
OK
 BOOST_STATIC_ASSERT(std::numeric_limits<UnsignedInt>::is_integer); // fails
here!
 /* Original combined test also fails.
  BOOST_STATIC_ASSERT((std::numeric_limits<UnsignedInt>::is_specialized
                        && std::numeric_limits<UnsignedInt>::is_integer
                        && !std::numeric_limits<UnsignedInt>::is_signed));
*/
public:
   // details here
};
} // namespace my_conditions

//myclass<unsigned int> m1; // this should be OK
//myclass<int> m2; // this should fail
//myclass<unsigned char> m3; // and so should this

int main()
{
typedef unsigned int unsignedInt;
cout << "bits " << sizeof(unsignedInt) * CHAR_BIT << endl;

cout << " is_specialized " << boolalpha <<
std::numeric_limits<unsignedInt>::is_specialized << endl;
cout << " is_integer " << std::numeric_limits<unsignedInt>::is_integer <<
endl;
cout << " is_unsigned " << !std::numeric_limits<unsignedInt>::is_signed <<
endl;

cout << " is_unsigned integer >=16 bits "
<< (std::numeric_limits<unsignedInt>::is_specialized &&
std::numeric_limits<unsignedInt>::is_integer
                        && !std::numeric_limits<unsignedInt>::is_signed)
                                                                                                << endl;

my_conditions::myclass<unsigned int> m1; // this should be OK

   return 0;
} // main

/*

bits 32
 is_specialized true
 is_integer true
 is_unsigned true
 is_unsigned integer >=16 bits true
Press any key to continue

*/

/*
------ Build started: Project: static_assert3, Configuration: Debug
Win32 ------

Compiling...
static_assert_example3.cpp
static_assert_example3.cpp(36) : error C2027: use of undefined type
'boost::STATIC_ASSERTION_FAILURE<x>'
        with
        [
            x=false
        ]
        static_assert_example3.cpp(44) : see reference to class template
instantiation 'my_conditions::myclass<UnsignedInt>' being compiled
static_assert_example3.cpp(36) : error C2371:
'my_conditions::myclass<UnsignedInt>::boost_static_assert_typedef_' :
redefinition; different basic types
        static_assert_example3.cpp(31) : see declaration of
'my_conditions::myclass<UnsignedInt>::boost_static_assert_typedef_'

Build log was saved at
"file://j:\Cpp\boost_test\static_assert3\Debug\BuildLog.htm"
static_assert3 - 2 error(s), 0 warning(s)

---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped

*/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk