9

私はこれを見るテンプレートメソッドを持つクラスを取得しました:

struct undefined {};

template<typename T> struct is_undefined : mpl::false_ {};

template<> struct is_undefined<undefined> : mpl::true_ {};

template<class C>
struct foo {
        template<class F, class V>
        typename boost::disable_if<is_undefined<C> >::type
            apply(const F &f, const V &variables) {
        }

        template<class F, class V>
        typename boost::enable_if<is_undefined<C> >::type
            apply(const F &f, const V &variables) {
        }
};

明らかに、両方のテンプレートがインスタンス化され、コンパイル時エラーが発生します。テンプレートメソッドのインスタンス化は、無料の関数のインスタンス化とは異なりますか?これを別の方法で修正しましたが、何が起きているのか知りたいです。私が考えることができる唯一のことは、この動作を引き起こす可能性があります。有効化条件は、即時のテンプレート引数ではなく、クラステンプレート引数に依存します。

ありがとうございました

4

1 に答える 1

12

あなたCはの控除に参加していませんapply。コードが失敗する理由の詳細については、この回答を参照してください。

次のように解決できます。

template<class C>
struct foo {    
        template<class F, class V>
        void apply(const F &f, const V &variables) { 
            apply<F, V, C>(f, variables); 
        }

private:
        template<class F, class V, class C1>
        typename boost::disable_if<is_undefined<C1> >::type
            apply(const F &f, const V &variables) {
        }

        template<class F, class V, class C1>
        typename boost::enable_if<is_undefined<C1> >::type
            apply(const F &f, const V &variables) {
        }
};
于 2010-05-30T04:23:06.273 に答える