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 回答