重载运算符的模板:错误:重载'operator*'必须至少有一个类或枚举类型的参数
Templates to overload operators: error: overloaded 'operator*' must have at least one parameter of class or enumeration type
我想在具有各种分量的结构上定义一个向量空间(即分量加法和标量乘法)。因此,在这个简短的示例中,我为特定结构重载operator*=
,然后在任何operator*
的模板中使用它。
以下代码编译并运行:
#include <assert.h>
struct myfloat3 { float x, y, z; };
myfloat3 operator*=(myfloat3& a, const float b) {
a.x *= b; a.y *= b; a.z *= b;
return a;
}
template<typename Pt>
Pt operator*(const Pt& a, const float b) {
auto prod = a;
prod *= b;
return prod;
}
// template<typename Pt>
// Pt operator*(const float b, const Pt& a) {
// auto prod = a;
// prod *= b;
// return prod;
// }
int main(int argc, char const *argv[]) {
myfloat3 x {1, 2, 3};
assert((x*3.0f).x == 3);
return 0;
}
但是,如果我取消注释另一个排序的第二个重载float * myfloat3
,我就会得到
$ g++ -std=c++11 sandbox/overload.cpp
sandbox/overload.cpp:20:4: error: overloaded 'operator*' must have at least one
parameter of class or enumeration type
Pt operator*(const float b, const Pt& a) {
^
sandbox/overload.cpp:30:14: note: in instantiation of function template
specialization 'operator*<float>' requested here
assert((x*3.0f).x == 3);
^
/usr/include/assert.h:93:25: note: expanded from macro 'assert'
(__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __FILE__, __LINE...
^
1 error generated.
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
然而,使用不同的编译器可以工作:
$ g++ --version
g++ (Ubuntu 4.9.2-10ubuntu13) 4.9.2
$ g++ -std=c++11 sandbox/soverload.cpp
有什么想法吗?
使用带有enable_if
的特征类来控制何时启用模板:
template<typename Pt> struct Is_vector: public std::false_type {};
template<> struct Is_vector<float3>: public std::true_type {};
template<typename Pt>
typename std::enable_if<Is_vector<Pt>::value, Pt>::type
operator*(const Pt& a, const float b) {
auto prod = a;
prod *= b;
return prod;
}
template<typename Pt>
typename std::enable_if<Is_vector<Pt>::value, Pt>::type
operator*(const float b, const Pt& a) {
auto prod = a;
prod *= b;
return prod;
}
请参阅https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/enable-if和Thrust reduction和重载运算符-(const float3&,const float 3&won';不要为了解释而编译。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在学习数据结构之前对STL有一个了解是好的吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 我的输出中有一个额外的 0,为什么会这样
- 节点是否为空,即使它有一个值?
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 在 Stream C++ 文本之前有一个额外的换行符
- 我可以有一个 ELI5 作为参考和指针以及何时使用它们吗?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 在类中使用'const'和在C++中使用运算符重载是否有一个好的规则?
- 我可以有一个函数/方法通过引用传递参数和重载通过值传递它在c++中
- 什么意思是"错误:重载的'运算符*'必须至少有一个类或枚举类型的参数"
- 重载运算符的模板:错误:重载'operator*'必须至少有一个类或枚举类型的参数