2010-05-30 95 views
9

我得到了类,看起来在这个模板方法:的boost :: enable_if类模板方法

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) { 
     } 
}; 

显然,这两个模板被实例化,导致编译时错误。 是与实例化自由函数不同的模板方法的实例吗? 我已经解决了这个问题,但我想知道什么是。 我能想到的唯一的事情可能会导致这种现象,使条件不依赖直接的模板参数,而是类模板参数

谢谢

回答

12

C不参与扣除apply。有关代码失败的原因的详细信息,请参见this answer

可以解决这个问题是这样的:

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) { 
     } 
}; 
+0

谢谢你一次。 – Anycorn 2010-05-30 04:26:43