gpt4 book ai didi

c++ - 使用 C++ 函数库时无法正确实例化模板

转载 作者:搜寻专家 更新时间:2023-10-31 01:56:33 24 4
gpt4 key购买 nike

我正在尝试创建一类固定大小的 vector ,主要用于几何目的, vector 长度不变的地方:

template<typename T, int n>
class FixedVector
{
private:
T m_rV[n]; // this is the only data member
public:
// class function members
...
}

这将具有编译器检查使用不兼容大小的 vector 的操作的优势。

我在尝试为此类构建 operator* 时遇到问题(注意:它不是成员)。此运算符应将 vector 乘以标量,例如 3*[1,2,3]=[3,6,9]。

template<typename T, int n>
FixedVector<T,n> operator*(const T &rX, const FixedVector<T,n> &cV) const
{ typename std::pointer_to_binary_function<T,T,T> op=(util::times<T>);
FixedVector<T,n> cT(cV, std::bind1st(op, rX));
return cT;
}

其中 times 是 vector 的标量成员的乘法函数

template<typename T>
inline T times(const T &t1, const T &t2)
{ return t1*t2;
}

第4行构造函数的代码是

template<typename T, int n>
FixedVector<T,n>::FixedVector(const T rV[n], T (*f)(const T &))
{ util::copy(m_rV, rV, n, f);
}

pointer_to_binary_function 和 bind1st 是头文件中的 STL 函数(能提供帮助的人应该已经知道了)。

我在 Visual Studio 2005 中调用时遇到以下编译器错误

    util::FixedVector<int,4> x; 3*x;

:

fixed_vector.hpp(212) : error 2440:
'initializing' : cannot convert from 'T (__cdecl *)(const T &,const T &)'
to 'std::pointer_to_binary_function<_Arg1,_Arg2,_Result>'
with
[
_Arg1=int,
_Arg2=int,
_Result=int
]
No constructor could take the source type, or constructor overload resolution was ambiguous
testproject.cpp(18) : see reference to function template instantiation 'util::FixedVector<T,n> util::operator *<T,4>(const T &,const util::FixedVector<T,n> &)' being compiled
with
[
T=int,
n=4
]

看来 类型名 std::pointer_to_binary_function被正确实例化为 std::pointer_to_binary_function但是,时间仍然保留其基本签名: 'T (__cdecl *)(const T &,const T &)


--- 经过一些编辑---------------------------------------- --------------------------

有人向我指出,我的构造函数请求一个普通函数作为参数的函数:T (*)(const T &t1, const T &t2) 并且不会接受 STL 函数对象。链接在这里STL for_each作为如何进行更正的指南。

我从 util::copy 函数开始改变,由构造函数调用。

来自: 模板 void copy(T *dst, const T *src, size_t n, T (*f)(const T &)) { for (; n>0; n--, dst++, src++) { *dst = f(*src); }

变成了

    template<typename T, typename Function>
void copy(T *dst, const T *src, size_t n, Function f)
{ for (; n>0; n--, dst++, src++)
{ *dst = (*f)(*src);
} }

然后,构造函数本身被模板化。来自:

template<typename T, int n>
FixedVector<T,n>::FixedVector(const T rV[n], T (*f)(const T &))
{ util::copy(m_rV, rV, n, f);
}

现在

     template<typename T, int n>
template<class Function>
FixedVector<T,n>::FixedVector(const FixedVector<T,n> &cV, Function f)
{ util::copy(m_rV, cV.m_rV, n, f);
}

还在模板实例化参数中添加了一些常量:

template<typename T, int n>
FixedVector<T,n> operator*(const T &rX, const FixedVector<T,n> &cV)
{ typename std::pointer_to_binary_function<const T,const T,T> op(times<T>);
FixedVector<T,n> cT(cV, std::bind1st(op, rX));
return cT;
}

但是我仍然得到同样的错误(只是T已经被const T替换了;注意添加&来表示引用(const T&)会触发错误而且模板似乎有问题,Boost和TR1创建特殊的解决方案来处理这个问题 - 请参阅 Wikipedia TR1 reference wrapper)。

错误的确切行是这样的:

typename std::pointer_to_binary_function<const T,const T,T> op(times<T>);

所以我什至没有到达构造函数。

如果有一些额外的想法,我将不胜感激。

最佳答案

你的函数 util::times有签名:

T times(const T&, const T&)

但在这一行中:

typename std::pointer_to_binary_function<T,T,T> op=(util::times<T>);

std::pointer_to_binary_function 的构造函数正在期待:

T times(T, T)

您可以使用 const T& 来解决这个问题作为参数模板参数:

typename std::pointer_to_binary_function<const T&, const T&, T> op(util::times<T>);

请注意,我删除了 =在这里使用显式构造函数符号。至少在(我的)GCC 中,赋值语法被编译器拒绝。

由于您正在创建一个执行乘法的二元仿函数,而不是自定义的 times函数和仿函数包装器,你可以直接使用 std::multiplies实现相同的目标:

std::multiplies<T> op;

请注意,在您的下一行中,

FixedVector<T,n> cT(cV, std::bind1st(op, rX));

你正在调用一个接受 FixedVector<T, n> 的构造函数作为第一个参数,一元仿函数作为第二个。这与您发布的构造函数代码不兼容:

template<typename T, int n>
FixedVector<T,n>::FixedVector(const T rV[n], T (*f)(const T &))
{ util::copy(m_rV, rV, n, f);
}

因为这个接受一个普通数组和一个函数指针。

关于c++ - 使用 C++ 函数库时无法正确实例化模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6925917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com