宣言&定義:
// 引数をそのまま返す
template<typename T>
- inline T cast( typename boost::call_traits<T>::param_type x ) { return x; }
+ inline T cast( typename call_traits<T>::param_type x ) { return x; }
機能:
暗黙変換を明示的に行うためのキャストです。 cast<void*>( 0 ) のように使います。
*/
-#include <boost/call_traits.hpp>
+#include "call_traits.hpp"
namespace gintenlib
{
// 暗黙キャスト関数
template<typename T>
- inline T cast( typename boost::call_traits<T>::param_type x )
+ inline T cast( typename call_traits<T>::param_type x )
{
return x;
}
*/
+// タイプ数削減のための using 宣言
+#include "call_traits.hpp"
+
#include <boost/range.hpp>
-#include <boost/call_traits.hpp>
namespace gintenlib
{
typename Delim = const char*, typename Post = const char* >
struct list_format_impl_
{
- typedef typename boost::call_traits<InIte>::param_type inite_param_t;
- typedef typename boost::call_traits<Pre>::param_type pre_param_t;
- typedef typename boost::call_traits<Delim>::param_type delim_param_t;
- typedef typename boost::call_traits<Post>::param_type post_param_t;
+ typedef typename call_traits<InIte>::param_type inite_param_t;
+ typedef typename call_traits<Pre>::param_type pre_param_t;
+ typedef typename call_traits<Delim>::param_type delim_param_t;
+ typedef typename call_traits<Post>::param_type post_param_t;
// constructor
list_format_impl_( inite_param_t begin, inite_param_t end,
pre_param_t pre, delim_param_t delim, post_param_t post )
: begin_(begin), end_(end), pre_(pre), delim_(delim), post_(post) {}
- typename boost::call_traits<InIte>::value_type begin_, end_;
- typename boost::call_traits<Pre>::value_type pre_;
- typename boost::call_traits<Delim>::value_type delim_;
- typename boost::call_traits<Post>::value_type post_;
+ typename call_traits<InIte>::value_type begin_, end_;
+ typename call_traits<Pre>::value_type pre_;
+ typename call_traits<Delim>::value_type delim_;
+ typename call_traits<Post>::value_type post_;
// 出力の実装
// テンプレートにより std::ostream に限らず出力できます
*/
+// operator() で使う
+#include "list_format.hpp"
+// タイプ数削減のための using 宣言
+#include "call_traits.hpp"
+
#include <iterator>
#include <cassert>
#include <algorithm> // for swap()
#include <boost/range.hpp>
-#include <boost/call_traits.hpp>
-#include "list_format.hpp"
namespace gintenlib
{
template< typename Elem >
class basic_list_formatter
{
- typedef typename boost::call_traits<Elem>::param_type param_type;
+ typedef typename call_traits<Elem>::param_type param_type;
public:
- typedef typename boost::call_traits<Elem>::value_type element_type;
+ typedef typename call_traits<Elem>::value_type element_type;
basic_list_formatter()
: pre("( "), delim(", "), post(" )"), iterator_count_(0) {}
--- /dev/null
+#include "../gintenlib/cast.hpp"
+
+// boost の単体テストフレームワーク
+#include <boost/test/minimal.hpp>
+
+using gintenlib::cast;
+
+void test1()
+{
+ // オーバーロードのチェック
+ struct
+ {
+ int operator()(int) { return 1; }
+ int operator()(double) { return 2; }
+ int operator()(void*) { return 3; }
+ } f;
+
+ BOOST_CHECK( f(0) == 1 );
+ BOOST_CHECK( f( cast<double>(0) ) == 2 );
+ BOOST_CHECK( f( cast<void*>(0) ) == 3 );
+}
+
+// ユーザ定義型
+struct base {};
+struct derived : base {};
+
+void test2()
+{
+ struct
+ {
+ int operator()( const base& ) { return 1; }
+ int operator()( const derived& ) { return 2; }
+
+ } f;
+
+ base b; derived d;
+
+ // 特に意味のないチェック
+ BOOST_CHECK( f( b ) == 1 );
+ BOOST_CHECK( f( d ) == 2 );
+
+ // derived -> base へのキャスト
+ // 値
+ BOOST_CHECK( f( cast<base >( d) ) == 1 );
+ // ポインタ
+ BOOST_CHECK( f( *cast<base*>(&d) ) == 1 );
+ // 参照
+ BOOST_CHECK( f( cast<base&>( d) ) == 1 );
+
+ // base -> derived へのキャスト(ダメ)
+ // 値
+ // BOOST_CHECK( f( cast<derived >( b) ) == 2 );
+ // ポインタ
+ // BOOST_CHECK( f( *cast<derived*>(&b) ) == 2 );
+ // 参照
+ // BOOST_CHECK( f( cast<derived&>( b) ) == 2 );
+}
+
+
+int test_main( int argc, char* argv[] )
+{
+ // コンパイルが通った時点で殆どテスト成功
+ test1();
+ test2();
+
+ return 0;
+}