Home / ぼやきごと / 2011-09-15 / テンプレートクラスとその派生クラスに対する特殊化の適用
テンプレートクラスとその派生クラスに対する特殊化の適用

テンプレートクラスとその派生クラスに対する特殊化の適用

Prev: [派生クラスに対するテンプレート特殊化の適用]

前回の記事において、あるクラスとその派生クラスに対してテンプレートの特殊化を適用させる方法を示しました。
継承元が普通のクラスである場合は boost ライブラリを用いることで容易に実現可能でした。

では、継承元がテンプレートクラスである場合はどうでしょうか。
例えば次のような場合です。

すべて開くすべて閉じる
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 
 
 
 
-
!
-
!
 
 
-
!
 
 
-
!
 
-
!
-
!
 
-
|
-
|
!
!
 
-
|
!
 
 
-
|
-
|
!
!
 
 
-
!
 
-
|
|
|
|
|
|
!
#include <boost/type_traits/is_base_of.hpp>
#include <boost/utility/enable_if.hpp>
#include <iostream>
 
//----------
 
//! Base<T> クラス
template<class T>
class Base { };
 
//! Base<T> クラスの派生クラス
template<class T>
class Derived : public Base<T> { };
 
//! Base<bool> クラスの派生クラス
class DerivedBool : public Base<bool> { };
 
//----------
 
//! 関数オブジェクトテンプレート(基本型)
template<class T, class = void>
struct Func
{
    void operator()() const
    {
        std::cout << "Normal" << std::endl;
    }
};
 
// Base<T> クラス(型 T は任意)から派生するあらゆるクラスに
// 適用するにはどう特殊化すればいい??
#if 0
template<class T>
struct Func< T, ??? >
{
    void operator()() const
    {
        std::cout << "Special" << std::endl;
    }
};
#endif
 
//----------
 
int main(int, char**)
{
    Func<int>            a; a();    // Normal を期待
    Func< Base<char> >   b; b();    // Special を期待
    Func< Derived<int> > c; c();    // Special を期待
    Func<DerivedBool>    d; d();    // Special を期待
 
    return 0;
}

解法は(私の頭では)2種類考え付きます。

Base<T> クラスが変更可能である場合は簡単で、 Base<T> クラスを更に別のテンプレートでないクラスから継承させれば済みます。
継承の大元がテンプレートクラスでなくなってしまえば、前回の記事の手法がそのまま使えます。

すべて開くすべて閉じる
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 
 
 
 
-
!
-
!
 
-
!
 
 
-
!
 
 
-
!
 
-
!
-
!
 
-
|
-
|
!
!
 
-
!
 
 
 
-
|
-
|
!
!
 
-
!
 
-
|
|
|
|
|
|
!
#include <boost/type_traits/is_base_of.hpp>
#include <boost/utility/enable_if.hpp>
#include <iostream>
 
//----------
 
//! BaseT クラス
class BaseT { };
 
//! Base<T> クラス
template<class T>
class Base : public BaseT { };
 
//! Base<T> クラスの派生クラス
template<class T>
class Derived : public Base<T> { };
 
//! Base<bool> クラスの派生クラス
class DerivedBool : public Base<bool> { };
 
//----------
 
//! 関数オブジェクトテンプレート(基本型)
template<class T, class = void>
struct Func
{
    void operator()() const
    {
        std::cout << "Normal" << std::endl;
    }
};
 
//! BaseT クラスとその派生クラス用に部分特殊化した Func
template<class T>
struct Func<
    T,
    typename boost::enable_if< boost::is_base_of<BaseT, T> >::type >
{
    void operator()() const
    {
        std::cout << "Special" << std::endl;
    }
};
 
//----------
 
int main(int, char**)
{
    Func<int>            a; a();    // Normal を期待
    Func< Base<char> >   b; b();    // Special を期待
    Func< Derived<int> > c; c();    // Special を期待
    Func<DerivedBool>    d; d();    // Special を期待
 
    return 0;
}

Base<T> クラスが変更不可能である場合、前回の記事の最初の解法でも用いた手法である「可変長引数関数による型の切り分け」を行う必要があります。
boost ライブラリのメタプログラミング手法に準じた書き方をすると次のようになります。

すべて開くすべて閉じる
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 
 
 
 
 
 
 
-
!
-
!
 
 
-
!
 
 
-
!
 
-
!
 
-
-
!
|
-
|
-
!
|
-
!
|
|
|
|
|
-
!
|
|
-
!
|
!
!
 
-
|
!
 
 
 
 
 
-
!
 
-
!
-
!
 
-
|
-
|
!
!
 
-
!
 
 
 
-
|
-
|
!
!
 
-
!
 
-
|
|
|
|
|
|
!
#include <boost/type_traits/is_base_of.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/detail/yes_no_type.hpp>
#include <boost/utility/enable_if.hpp>
#include <iostream>
 
//----------
 
//! Base<T> クラス
template<class T>
class Base { };
 
//! Base<T> クラスの派生クラス
template<class T>
class Derived : public Base<T> { };
 
//! Base<bool> クラスの派生クラス
class DerivedBool : public Base<bool> { };
 
//----------
 
namespace
{
    //! TDerived 型が TBase<T> 型およびその派生型か否かのチェッククラス
    template<class TDerived>
    class BaseTDerivedChecker
    {
    private:
        //! チェック用可変長引数関数
        static boost::type_traits::no_type check(...);
 
        //! Base<T> 型およびその派生型の場合のみ定義されるオーバロード
        template<class T>
        static boost::type_traits::yes_type check(
            const Base<T>&,
            typename boost::enable_if<
                boost::is_base_of<Base<T>, TDerived> >::type* = 0);
 
        //! チェック用静的メンバ変数
        static typename boost::add_reference<TDerived>::type d;
 
    public:
        //! チェック結果
        static const bool value =
            (sizeof(check(d)) == sizeof(boost::type_traits::yes_type));
    };
}
 
//! TDerived 型が TBase<T> 型およびその派生型か否か調べるメタ関数
//! boost::enable_if 等に渡して使える
template<class TDerived>
struct IsBaseTDerived
    :
    public boost::integral_constant<
        bool,
        BaseTDerivedChecker<TDerived>::value>
{
};
 
//----------
 
//! 関数オブジェクトテンプレート(基本型)
template<class T, class = void>
struct Func
{
    void operator()() const
    {
        std::cout << "Normal" << std::endl;
    }
};
 
//! Base<T> クラスとその派生クラス用に部分特殊化した Func
template<class T>
struct Func<
    T,
    typename boost::enable_if< IsBaseTDerived<T> >::type >
{
    void operator()() const
    {
        std::cout << "Special" << std::endl;
    }
};
 
//----------
 
int main(int, char**)
{
    Func<int>            a; a();    // Normal を期待
    Func< Base<char> >   b; b();    // Special を期待
    Func< Derived<int> > c; c();    // Special を期待
    Func<DerivedBool>    d; d();    // Special を期待
 
    return 0;
}

BaseTDerivedChecker<TDerived> テンプレートクラスでは、少なくとも1つの型 T について型 TDerived が次の要件を満たす場合のみ静的メンバ関数 check のオーバロードが定義されます。

  • TDerived の参照型が型 const Base<T>& に変換可能である。
  • TDerivedBase<T> クラスまたはその派生クラスである。

いずれの型 T についても型 TDerived が上述の条件を満たさない場合、オーバロードは定義されません。

なお、前回の記事も含め、例示したコードは古いコンパイラではコンパイルエラーとなる可能性があります。
私自身は Visual C++ 2008 でコンパイルして動作確認しています。

Category: [C++][boost][プログラミング] - 2011-09-15 00:14:03