我得到了一个模板方法类,看看这个:
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) {
}
};
Run Code Online (Sandbox Code Playgroud)
显然,两个模板都被实例化,导致编译时错误.是否实例化模板方法不同于自由函数的实例化?我已经解决了这个问题,但我想知道是什么.我唯一能想到的可能会导致这种行为,启用条件不依赖于立即模板参数,而是依赖于类模板参数
谢谢
我正在编写一个简单的向量类,我希望有一些成员函数只能在某些长度的向量中使用(例如,3元素向量的交叉乘积).我偶然发现了std :: enable_if,看起来它可能能够做我想要的,但我似乎无法让它正常工作.
#include <iostream>
#include <type_traits>
template<typename T, unsigned int L>
class Vector
{
private:
T data[L];
public:
Vector<T,L>(void)
{
for(unsigned int i = 0; i < L; i++)
{
data[i] = 0;
}
}
T operator()(const unsigned int i) const
{
return data[i];
}
T& operator()(const unsigned int i)
{
return data[i];
}
Vector<typename std::enable_if<L==3, T>::type, L> cross(const Vector<T,L>& vec2) const
{
Vector<T,L> result;
result(0) = (*this)(1) * vec2(2) - (*this)(2) * vec2(1);
result(1) = (*this)(2) * vec2(0) …Run Code Online (Sandbox Code Playgroud)