function binding

[SVN r34175]
This commit is contained in:
Arkadiy Vertleyb
2006-06-05 02:32:41 +00:00
parent 8ce982a048
commit a8112fe31d
7 changed files with 116 additions and 25 deletions

View File

@ -11,6 +11,8 @@
# include <boost/config.hpp>
# include <boost/detail/workaround.hpp>
# include <boost/mpl/int.hpp>
# include <boost/type_traits/is_function.hpp>
# include <boost/utility/enable_if.hpp>
namespace boost
{
@ -145,8 +147,28 @@ namespace boost
BOOST_TYPEOF_NEXT_INDEX(next);
};
template<class T>
struct sizer
{
typedef char(*type)[encode_type<T>::value];
};
# if BOOST_WORKAROUND(BOOST_MSVC,>=1300)
template<typename T> typename disable_if<
typename is_function<T>::type,
typename sizer<T>::type>::type encode_start(T const&);
template<typename T> typename enable_if<
typename is_function<T>::type,
typename sizer<T>::type>::type encode_start(T&);
# else
template<typename T>
char (*encode_start(T const&))[encode_type<T>::value];
typename sizer<T>::type encode_start(T const&);
# endif
}
}

57
include/boost/typeof/native.hpp Executable file
View File

@ -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<class T>
T& ensure_obj(const T&);
}}
#else
#include <boost/type_traits/is_function.hpp>
#include <boost/utility/enable_if.hpp>
namespace boost { namespace type_of {
template<typename T>
typename enable_if<is_function<T>, T&>::type
ensure_obj(T&);
template<typename T>
typename disable_if<is_function<T>, 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

View File

@ -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<class T> 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 <boost/typeof/native.hpp>
#else
# error typeof configuration error
#endif
#include <boost/typeof/message.hpp>
#include <boost/typeof/binding_workaround.hpp>
// auto
#define BOOST_AUTO(Var, Expr) BOOST_TYPEOF(Expr) Var = Expr

View File

@ -10,6 +10,8 @@
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/typeof/encode_decode.hpp>
#include <boost/typeof/vector.hpp>
#include <boost/type_traits/is_function.hpp>
#include <boost/utility/enable_if.hpp>
#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<class V,class T>
sizer<typename encode_type<V, T>::type> encode(const T&);
}}
#undef BOOST_TYPEOF_sizer_item
//
namespace boost { namespace type_of {
template<class V, class T>
typename enable_if<
typename is_function<T>::type,
sizer<typename encode_type<V, T>::type> >::type encode(T&);
template<class V, class T>
typename disable_if<
typename is_function<T>::type,
sizer<typename encode_type<V, T>::type> >::type encode(const T&);
}}
//
namespace boost { namespace type_of {
template<class V>

View File

@ -4,3 +4,12 @@ BOOST_STATIC_ASSERT(boost::type_of::test<double(*)()>::value);
BOOST_STATIC_ASSERT(boost::type_of::test<double(*)(int, double, short, char*, bool, char, float, long, unsigned short)>::value);
BOOST_STATIC_ASSERT(boost::type_of::test<void(*)()>::value);
BOOST_STATIC_ASSERT(boost::type_of::test<void(*)(int, double, short, char*, bool, char, float, long, unsigned short)>::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<BOOST_TYPEOF(cptr), PTR>::value));

View File

@ -2,3 +2,11 @@
BOOST_STATIC_ASSERT(boost::type_of::test<void(&)()>::value);
BOOST_STATIC_ASSERT(boost::type_of::test<int(&)(int, short)>::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<BOOST_TYPEOF(fref), int(double)>::value));

View File

@ -8,7 +8,7 @@ void do_int(int) {}
struct {
template<typename T>
T operator[](const T&) {}
T operator[](const T& n) {return n;}
} int_p;