<gintenlib/call_traits.hpp>
- call_traits : boost::call_traits +α
+ call_traits : boost::call_traits
宣言:
- template<typename T>
- struct call_traits
- : boost::call_traits<T>
- {
- typedef T type;
- };
+ using boost::call_traits;
機能:
- boost::call_traits に「与えられた型をそのまま返す」メタ関数の機能を加えたものです。
- reference や param_type の取得もしたいし、メタ関数としても使いたい、なんて局面で使います。
- 単純に boost::call_traits は gintenlib 内での使用が多いので、短縮として使う意味もあります。
+ boost::call_traits への using 宣言です。
*/
namespace gintenlib
{
- // typename call_traits<T>::type は T に等しい
- template<typename T>
- struct call_traits
- : boost::call_traits<T>
- {
- typedef T type;
- };
+ using boost::call_traits;
} // namespace gintenlib
--- /dev/null
+#ifndef GINTENLIB_INCLUDED_IDENTITY_HPP_
+#define GINTENLIB_INCLUDED_IDENTITY_HPP_
+
+/*
+
+ <gintenlib/identity.hpp>
+
+ identity : boost::call_traits + boost::mpl::identity
+
+ 宣言:
+ template<typename T>
+ struct identity
+ : boost::call_traits<T>
+ {
+ typedef T type;
+ };
+
+ 機能:
+ boost::call_traits に「与えられた型をそのまま返す」メタ関数の機能を加えたものです。
+ reference や param_type の取得もしたいし、メタ関数としても使いたい、なんて局面で使います。
+ 実装補助用です。
+
+*/
+
+#include "call_traits.hpp"
+
+namespace gintenlib
+{
+ // gintenlib::identity = boost::call_traits + boost::mpl::identity
+ template<typename T>
+ struct identity
+ : call_traits<T>
+ {
+ typedef T type;
+ };
+
+} // namespace gintenlib
+
+
+#endif // #ifndef GINTENLIB_INCLUDED_IDENTITY_HPP_
--- /dev/null
+#include "../gintenlib/identity.hpp"
+
+// boost の単体テストフレームワーク
+#include <boost/test/minimal.hpp>
+
+// boost の該当ヘッダをインクルード
+#include <boost/mpl/identity.hpp>
+#include <boost/call_traits.hpp>
+
+#include <boost/mpl/assert.hpp>
+#include <boost/type_traits/is_same.hpp>
+
+// 二つの型が「等しい」ことをコンパイル時&一応実行時にチェックする
+template<typename T1, typename T2>
+bool is_equal()
+{
+ BOOST_MPL_ASSERT(( boost::is_same<T1, T2> ));
+ return boost::is_same<T1, T2>::value;
+}
+
+// 型 T に対して動作をチェックする
+template<typename T>
+void test()
+{
+ // gintenlib::identity<T>::type は T 自身に等しい
+ BOOST_CHECK(( is_equal<T, typename gintenlib::identity<T>::type>() ));
+
+ // value_type, param_type, reference, const_reference は、boost::call_traits と同じ
+ #define GEN_TEST_( xxx ) \
+ BOOST_CHECK(( is_equal<typename boost::call_traits<T>::xxx, \
+ typename gintenlib::identity<T>::xxx >() ));
+
+ GEN_TEST_( value_type );
+ GEN_TEST_( param_type );
+ GEN_TEST_( reference );
+ GEN_TEST_( const_reference );
+
+ #undef GEN_TEST_
+}
+
+#include <iostream>
+
+int test_main( int argc, char* argv[] )
+{
+ // 組み込み型
+ test<int>();
+ // オブジェクト
+ test<std::ostream>();
+ // ポインタ
+ test<int*>();
+ // 参照
+ test<std::ostream&>();
+
+ return 0;
+}