问题描述
我买了模板方法类,看起来这个:
I got class with template methods that looks at this:
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) {
}
};
显然,这两个模板实例化,从而导致编译时错误。
是从自由函数实例化不同的模板方法实例?
我有不同的解决了这个问题,但我想知道是什么了。
我能想到的唯一的事情可能会导致此行为,使条件不依赖直接的模板参数,而类模板参数
apparently, both templates are instantiated, resulting in compile time error.is instantiation of template methods different from instantiation of free functions?I have fixed this differently, but I would like to know what is up.the only thing I can think of that might cause this behavior, enabling condition does not depend immediate template arguments, but rather class template arguments
感谢您
推荐答案
您 C
不参与扣除适用
。见this回答为您为什么code未能更深的解释。
Your C
does not participate in deduction for apply
. See this answer for a deeper explanation of why your code fails.
您可以解决这个问题是这样的:
You can resolve it like this:
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) {
}
};
这篇关于提高:: enable_if类模板方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!