我正在编写一个简单的向量类,我希望有一些仅在某些长度的向量中可用的成员函数(例如,对于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) - (*this)(0) * vec2(2);
result(2) = (*this)(0) * vec2(1) - (*this)(1) * vec2(0);
return result;
}
};
int main(void)
{
Vector<double,3> v1;
Vector<double,3> v2;
Vector<double,3> v3;
//Vector<double,4> v4;
v1(0) = 1;
v1(1) = 2;
v1(2) = 3;
v2(0) = 4;
v2(1) = 5;
v2(2) = 6;
v3 = v1.cross(v2);
std::cout << v3(0) << std::endl;
std::cout << v3(1) << std::endl;
std::cout << v3(2) << std::endl;
return 0;
}
上面的代码编译并正确运行,但是如果我取消注释Vector<double,4> v4
的声明,我在编译时遇到以下错误:
vec.cpp: In instantiation of ‘class Vector<double, 4u>’:
vec.cpp:46:22: required from here
vec.cpp:29:59: error: no type named ‘type’ in ‘struct std::enable_if<false, double>’
是否有人能够指出我哪里出错了?
答案 0 :(得分:8)
template<unsigned LL = L>
Vector<typename std::enable_if<LL==3 && L == 3, T>::type, LL>
cross(const Vector<T,LL>& vec2) const
{
Vector<T,L> result;
result(0) = (*this)(1) * vec2(2) - (*this)(2) * vec2(1);
result(1) = (*this)(2) * vec2(0) - (*this)(0) * vec2(2);
result(2) = (*this)(0) * vec2(1) - (*this)(1) * vec2(0);
return result;
}
PS。为什么这样工作?
变量v4
的定义导致类模板Vector
的隐式实例化,这反过来又导致类成员函数声明的隐式实例化(14.7.1)隐式实例化[temp.inst]#1)。当然,后一种实例化会导致错误。
如果我们改为将成员函数更改为成员模板,则根据相同的子句,此时成员模板本身将被实例化,并且此实例化或多或少地看起来像:< / p>
template<unsigned LL = 3>
Vector<typename std::enable_if<LL==3 && 3 == 3, double>::type, LL>
cross(const Vector<double,LL>& vec2) const;
这是一个完全有效的模板声明。我们此时并没有(我们不能)执行任何进一步的实例化。
但是,当我们尝试实际调用cross
时,毫无疑问,“需要成员/函数定义存在的上下文”,因此,根据(14.7.1隐式实例化[temp.inst] ]#2,#3),cross
模板(第二个cross
模板,一个是外部类模板实例化的结果)被隐式实例化,并给出std::enable_if
有机会做它的工作。作为旁注,这是SFINAE原则适用的情况。
PPS。进一步详细说明,虽然没有与OP问题直接相关,但仍然值得一提的是,为了处理类似的情况,并不总是需要将成员声明为模板。
在某些情况下,类模板的成员对于给定的实例化不是“有效”,但仍然可以实例化类模板,例如:
#include <type_traits>
template<typename T>
struct S
{
T x;
T foo () const { return x; }
typename std::remove_pointer<T>::type bar () const { return *x; }
};
S<int> x;
S<int *> y;
显然,在实例化S<int>
中,表达式*x
无效,因为x
的类型为int
。不过,这个程序是正确的。
重要的一点是,在隐式实例化期间,只会实例化成员的声明。在上面的例子中,实例化S<int>
导致声明 int bar() const;
被实例化,这是一个完全正确的声明。
当然,如果我们稍后尝试实例化S<int>::bar
的定义,就像在:
void f()
{
x.foo ();
// x.bar (); // error
y.foo ();
y.bar ();
}
我们会收到错误。
(这仍然遵循上述C ++标准的两段)