Skip to content
Snippets Groups Projects
Commit b81573fa authored by Christian Engwer's avatar Christian Engwer
Browse files

nullptr as proposed in

parent 592ff83a
No related branches found
No related tags found
No related merge requests found
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_NULLPTR_HH
#define DUNE_NULLPTR_HH
#if ! HAVE_NULLPTR
/**
\brief Fallback implementation of nullptr
see C++ proposal
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf
*/
const // this is a const object...
class nullptr_t { // of type nullptr_t
public:
template<class T> // convertible to any type
operator T*() const // of null non-member
{ return 0; } // pointer...
template<class C, class T> // or any type of null
operator T C::*() const // member pointer...
{ return 0; }
private:
void operator&() const; // whose address can't be taken
} nullptr = {}; // and whose name is nullptr
#endif // HAVE_NULLPTR
#endif // DUNE_NULLPTR_HH
......@@ -5,7 +5,7 @@ TESTPROGS = parsetest test-stack arraylisttest smartpointertest \
poolallocatortest settest gcdlcmtest streamtest \
bigunsignedinttest mpihelpertest singletontest mpicollcomm \
utilitytest lrutest \
smallobject \
smallobject nullptr-test \
testfassign1 testfassign2 testfassign3 \
testfassign4 \
testfassign_fail1 testfassign_fail2 testfassign_fail3 \
......@@ -14,7 +14,7 @@ TESTPROGS = parsetest test-stack arraylisttest smartpointertest \
# which tests to run
COMPILE_XFAIL=$(DUNE_COMMON_ROOT)/bin/xfail-compile-tests
COMPILE_XFAIL_TESTS = static_assert_test
COMPILE_XFAIL_TESTS = nullptr-test-fail static_assert_test
compile_XFAIL:
for i in $(COMPILE_XFAIL_TESTS); do \
......@@ -40,6 +40,10 @@ AM_LDFLAGS = $(LOCAL_LIBS)
# define the programs
smallobject_SOURCES = smallobject.cc
nullptr_test_SOURCES = nullptr-test.cc nullptr-test2.cc
nullptr_test_fail_SOURCES = nullptr-test.cc
nullptr_test_fail_CPPFLAGS = -DFAIL
static_assert_test_SOURCES = static_assert_test.cc
bigunsignedinttest_SOURCES=bigunsignedinttest.cc
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <config.h>
#include <dune/common/nullptr.hh>
void basic_tests()
{
typedef nullptr_t NULLPTR_T;
char* ch = nullptr; // ch has the null pointer value
char* ch2 = 0; // ch2 has the null pointer value
#ifdef FAIL
int n = nullptr; // error
#endif
int n2 = 0; // n2 is zero
if( ch == 0 ) ; // evaluates to true
if( ch == nullptr ) ; // evaluates to true
if( ch ) ; // evaluates to false
if( n2 == 0 ) ; // evaluates to true
ch = ch2;
#ifdef FAIL
if( n2 == nullptr ) ; // error
if( nullptr ) ; // error, no conversion to bool
if( nullptr == 0 ) ; // error
// arithmetic
nullptr = 0; // error, nullptr is not an lvalue
nullptr + 2; // error
#endif
}
int main()
{
basic_tests();
return 0;
}
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <config.h>
#include <dune/common/nullptr.hh>
......@@ -11,6 +11,7 @@ AC_DEFUN([DUNE_COMMON_CHECKS],
AC_REQUIRE([DUNE_CHECK_COMPILER])
AC_REQUIRE([GXX0X])
AC_REQUIRE([STATIC_ASSERT_CHECK])
AC_REQUIRE([NULLPTR_CHECK])
AC_REQUIRE([DUNE_LINKCXX])
AC_REQUIRE([DUNE_CHECKDEPRECATED])
AC_REQUIRE([DUNE_SET_MINIMAL_DEBUG_LEVEL])
......
......@@ -33,3 +33,21 @@ AC_DEFUN([STATIC_ASSERT_CHECK],[
fi
AC_LANG_POP
])
AC_DEFUN([NULLPTR_CHECK],[
AC_REQUIRE([AC_PROG_CXX])
AC_REQUIRE([GXX0X])
AC_LANG_PUSH([C++])
AC_MSG_CHECKING([whether nullptr is supported])
AC_TRY_COMPILE([],[typedef nullptr_t peng;
char* ch = nullptr;
], [
HAVE_NULLPTR=yes
AC_MSG_RESULT(yes)], [
HAVE_NULLPTR=no
AC_MSG_RESULT(no)])
if test "x$HAVE_NULLPTR" = xyes; then
AC_DEFINE(HAVE_NULLPTR, 1, [Define to 1 if nullptr is supported])
fi
AC_LANG_POP
])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment