Sil*_*lan 1 c++ g++ rvalue-reference c++11 ubuntu-14.04
我有一个带有以下声明的类:
\n\nclass IcoSphere\n{\n[...]\nprivate:\n int _addVertex(const glm::vec3 &p);\n int addVertex(glm::vec3 p);\n int addVertex(const glm::vec3 &&p);\n[...]\n};\nRun Code Online (Sandbox Code Playgroud)\n\n然后,我像这样调用“addVertex”:
\n\nIcoSphere sphere;\ndouble t = (1.0 +sqrt(5.0)) /2.0;\nsphere.addVertex(glm::vec3(-1,t,0));\nRun Code Online (Sandbox Code Playgroud)\n\n“addVertex”的参数显然不是引用,但 g++ 编译器会抛出以下错误:
\n\n./network/icosphere.cpp: In static member function \xe2\x80\x98static void IcoSphere::Create(glm::vec3&, float, std::vector<glm::tvec3<float, (glm::precision)0u> >&, int)\xe2\x80\x99:\n./network/icosphere.cpp:46:36: error: call of overloaded \xe2\x80\x98addVertex(glm::vec3)\xe2\x80\x99 is ambiguous\n sphere.addVertex(glm::vec3(-1,t,0));\n ^\n./network/icosphere.cpp:46:36: note: candidates are:\n./network/icosphere.cpp:19:5: note: int IcoSphere::addVertex(glm::vec3)\n int IcoSphere::addVertex(glm::vec3 p) {_addVertex(p);}\n ^\n./network/icosphere.cpp:20:5: note: int IcoSphere::addVertex(const vec3&&)\n int IcoSphere::addVertex(const glm::vec3 &&p) {_addVertex(p);}\n ^\nRun Code Online (Sandbox Code Playgroud)\n\n这对我来说没有多大意义,为什么它认为这是一个不明确的调用?
\n小智 5
当编译器处理函数重载决策时,它首先获取所有可行的函数,然后对它们进行排名并调用排名最高的函数。
然而,例如,在
type var;
void func(type);
void func(type&&);
func(var);
Run Code Online (Sandbox Code Playgroud)
两种func方法具有相同的排名。它们都是完全匹配的。不需要提升或隐式类型转换或其他任何东西。你的问题也是同样的情况。所以你可能想要改变
int addVertex(glm::vec3 p);
Run Code Online (Sandbox Code Playgroud)
到
int addVertex(const glm::vec3& p);
Run Code Online (Sandbox Code Playgroud)
因为你的目的并不是要改变它。有关重载解析和右值引用重载解析的更多信息,http://www.dcs.bbk.ac.uk/~roger/cpp/week20.htm、http : //yapb-soc.blogspot.com/2015/01/右值引用和函数.html
| 归档时间: |
|
| 查看次数: |
2177 次 |
| 最近记录: |