From a8112fe31d75a4b2c8d95866d6a4ace96d95f24e Mon Sep 17 00:00:00 2001 From: Arkadiy Vertleyb Date: Mon, 5 Jun 2006 02:32:41 +0000 Subject: [PATCH] function binding [SVN r34175] --- include/boost/typeof/msvc/typeof_impl.hpp | 24 +++++++++- include/boost/typeof/native.hpp | 57 +++++++++++++++++++++++ include/boost/typeof/typeof.hpp | 21 +-------- include/boost/typeof/typeof_impl.hpp | 20 ++++++-- test/function_ptr.cpp | 9 ++++ test/function_ref.cpp | 8 ++++ test/nested_typedef.cpp | 2 +- 7 files changed, 116 insertions(+), 25 deletions(-) create mode 100755 include/boost/typeof/native.hpp diff --git a/include/boost/typeof/msvc/typeof_impl.hpp b/include/boost/typeof/msvc/typeof_impl.hpp index c297e92..6b5a09b 100755 --- a/include/boost/typeof/msvc/typeof_impl.hpp +++ b/include/boost/typeof/msvc/typeof_impl.hpp @@ -11,6 +11,8 @@ # include # include # include +# include +# include namespace boost { @@ -145,8 +147,28 @@ namespace boost BOOST_TYPEOF_NEXT_INDEX(next); }; + template + struct sizer + { + typedef char(*type)[encode_type::value]; + }; + +# if BOOST_WORKAROUND(BOOST_MSVC,>=1300) + + template typename disable_if< + typename is_function::type, + typename sizer::type>::type encode_start(T const&); + + template typename enable_if< + typename is_function::type, + typename sizer::type>::type encode_start(T&); + +# else + template - char (*encode_start(T const&))[encode_type::value]; + typename sizer::type encode_start(T const&); + +# endif } } diff --git a/include/boost/typeof/native.hpp b/include/boost/typeof/native.hpp new file mode 100755 index 0000000..1c65a8c --- /dev/null +++ b/include/boost/typeof/native.hpp @@ -0,0 +1,57 @@ +// Copyright (C) 2006 Arkadiy Vertleyb +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_TYPEOF_NATIVE_HPP_INCLUDED +#define BOOST_TYPEOF_NATIVE_HPP_INCLUDED + +#ifndef MSVC_TYPEOF_HACK + +#ifdef BOOST_NO_SFINAE + +namespace boost { namespace type_of { + + template + T& ensure_obj(const T&); + +}} + +#else + +#include +#include + +namespace boost { namespace type_of { + + template + typename enable_if, T&>::type + ensure_obj(T&); + + template + typename disable_if, T&>::type + ensure_obj(const T&); + +}} + +#endif//BOOST_NO_SFINAE + +#define BOOST_TYPEOF(expr) BOOST_TYPEOF_KEYWORD(boost::type_of::ensure_obj(expr)) +#define BOOST_TYPEOF_TPL BOOST_TYPEOF + +#define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ + struct name {\ + typedef BOOST_TYPEOF_TPL(expr) type;\ + }; + +#define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ + struct name {\ + typedef BOOST_TYPEOF(expr) type;\ + }; + +#endif//MSVC_TYPEOF_HACK + +#define BOOST_TYPEOF_REGISTER_TYPE(x) +#define BOOST_TYPEOF_REGISTER_TEMPLATE(x, params) + +#endif//BOOST_TYPEOF_NATIVE_HPP_INCLUDED + diff --git a/include/boost/typeof/typeof.hpp b/include/boost/typeof/typeof.hpp index 0a18158..7669cab 100755 --- a/include/boost/typeof/typeof.hpp +++ b/include/boost/typeof/typeof.hpp @@ -136,31 +136,12 @@ #elif defined(BOOST_TYPEOF_NATIVE) # define BOOST_TYPEOF_TEXT "using native typeof" -# ifndef MSVC_TYPEOF_HACK - - namespace boost { namespace type_of { - template T& ensure_obj(const T&); - }} - -# define BOOST_TYPEOF(expr) BOOST_TYPEOF_KEYWORD(boost::type_of::ensure_obj(expr)) -# define BOOST_TYPEOF_TPL BOOST_TYPEOF -# define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ - struct name {\ - typedef BOOST_TYPEOF_TPL(expr) type;\ - }; -# define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ - struct name {\ - typedef BOOST_TYPEOF(expr) type;\ - }; -# endif -# define BOOST_TYPEOF_REGISTER_TYPE(x) -# define BOOST_TYPEOF_REGISTER_TEMPLATE(x, params) +# include #else # error typeof configuration error #endif #include -#include // auto #define BOOST_AUTO(Var, Expr) BOOST_TYPEOF(Expr) Var = Expr diff --git a/include/boost/typeof/typeof_impl.hpp b/include/boost/typeof/typeof_impl.hpp index e3a36e0..1fd661a 100755 --- a/include/boost/typeof/typeof_impl.hpp +++ b/include/boost/typeof/typeof_impl.hpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #define BOOST_TYPEOF_VECTOR(n) BOOST_PP_CAT(boost::type_of::vector, n) @@ -26,13 +28,25 @@ namespace boost { namespace type_of { BOOST_PP_REPEAT(BOOST_TYPEOF_LIMIT_SIZE, BOOST_TYPEOF_sizer_item, ~) }; - - template - sizer::type> encode(const T&); }} #undef BOOST_TYPEOF_sizer_item +// +namespace boost { namespace type_of { + + template + typename enable_if< + typename is_function::type, + sizer::type> >::type encode(T&); + + template + typename disable_if< + typename is_function::type, + sizer::type> >::type encode(const T&); + +}} +// namespace boost { namespace type_of { template diff --git a/test/function_ptr.cpp b/test/function_ptr.cpp index 2e08a15..be08c11 100755 --- a/test/function_ptr.cpp +++ b/test/function_ptr.cpp @@ -4,3 +4,12 @@ BOOST_STATIC_ASSERT(boost::type_of::test::value); BOOST_STATIC_ASSERT(boost::type_of::test::value); BOOST_STATIC_ASSERT(boost::type_of::test::value); BOOST_STATIC_ASSERT(boost::type_of::test::value); + +// check that const gets stripped from function pointer + +int foo(double); +typedef int(*PTR)(double); +typedef const PTR CPTR; +CPTR cptr = foo; + +BOOST_STATIC_ASSERT((boost::is_same::value)); diff --git a/test/function_ref.cpp b/test/function_ref.cpp index bc9f6ec..560ef53 100755 --- a/test/function_ref.cpp +++ b/test/function_ref.cpp @@ -2,3 +2,11 @@ BOOST_STATIC_ASSERT(boost::type_of::test::value); BOOST_STATIC_ASSERT(boost::type_of::test::value); + +// check that function values/refs can be bound + +int foo(double); +typedef int(&FREF)(double); +FREF fref = *foo; + +BOOST_STATIC_ASSERT((boost::is_same::value)); diff --git a/test/nested_typedef.cpp b/test/nested_typedef.cpp index d1bb3e4..129307e 100644 --- a/test/nested_typedef.cpp +++ b/test/nested_typedef.cpp @@ -8,7 +8,7 @@ void do_int(int) {} struct { template - T operator[](const T&) {} + T operator[](const T& n) {return n;} } int_p;