具有重载r值参考函数的歧义调用 [英] Ambiguous call with overloaded r-value reference function

查看:104
本文介绍了具有重载r值参考函数的歧义调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下声明的类:

  class IcoSphere 
{
[.. 。]
private:
int _addVertex(const glm :: vec3& p);
int addVertex(glm :: vec3 p);
int addVertex(const glm :: vec3& p);
[...]
};

然后,我这样称呼 addVertex:

  IcoSphere球形; 
double t =(1.0 + sqrt(5.0))/2.0;
sphere.addVertex(glm :: vec3(-1,t,0));

'addVertex'的参数显然不是引用,但是g ++编译器抛出以下内容错误:

  ./ network / icosphere.cpp:在静态成员函数'static void IcoSphere :: Create(glm :: vec3& ;,float,std :: vector< glm :: tvec3< float,(glm :: precision)0u>>& ;, int)':
./network/icosphere.cpp:46:36:错误:调用重载的'addVertex(glm :: vec3)'是模糊的
球形。addVertex(glm :: vec3(-1,t,0));
^
./network/icosphere.cpp:46:36:注意:候选者是:
./network/icosphere.cpp:19:5:注意:int IcoSphere :: addVertex( glm :: vec3)
int IcoSphere :: addVertex(glm :: vec3 p){_addVertex(p);}
^
./network/icosphere.cpp:20:5:注意:int IcoSphere :: addVertex(const vec3&&)
int IcoSphere :: addVertex(const glm :: vec3&& p){_addVertex(p);}
^

这对我来说意义不大,为什么考虑将其视为歧义呢?

解决方案

当编译器处理函数重载分辨率时,它首先获取所有可行的函数,然后对它们进行排名并调用排名最高的函数。 / p>

例如

  type var; 
void func(type);
void func(tpye&);
func(var);

这两种func方法的排名相同。它们都是完全匹配的。不需要提升或隐式类型转换或其他任何东西。您的问题也是如此。
所以您可能想要更改

  int addVertex(glm :: vec3 p); 

  int addVertex(const glm :: vec3& p); 

因为您不打算更改它。有关过载解析和右值引用过载解析的更多信息,请 http:// www .dcs.bbk.ac.uk /〜roger / cpp / week20.htm http://yapb-soc.blogspot.com/2015/01/rvalue-references-and-function.html


I have a class with the following declarations:

class IcoSphere
{
[...]
private:
    int _addVertex(const glm::vec3 &p);
    int addVertex(glm::vec3 p);
    int addVertex(const glm::vec3 &&p);
[...]
};

Then, I'm calling 'addVertex' like so:

IcoSphere sphere;
double t = (1.0 +sqrt(5.0)) /2.0;
sphere.addVertex(glm::vec3(-1,t,0));

The argument for 'addVertex' is obviously not a reference, and yet the g++-compiler throws the following error:

./network/icosphere.cpp: In static member function ‘static void IcoSphere::Create(glm::vec3&, float, std::vector<glm::tvec3<float, (glm::precision)0u> >&, int)’:
./network/icosphere.cpp:46:36: error: call of overloaded ‘addVertex(glm::vec3)’ is ambiguous
  sphere.addVertex(glm::vec3(-1,t,0));
                                    ^
./network/icosphere.cpp:46:36: note: candidates are:
./network/icosphere.cpp:19:5: note: int IcoSphere::addVertex(glm::vec3)
 int IcoSphere::addVertex(glm::vec3 p) {_addVertex(p);}
     ^
./network/icosphere.cpp:20:5: note: int IcoSphere::addVertex(const vec3&&)
 int IcoSphere::addVertex(const glm::vec3 &&p) {_addVertex(p);}
     ^

This doesn't make a whole lot of sense to me, why is it considering it an ambiguous call?

解决方案

When compiler dealing with function overloading resolution, it firstly gets all the viable functions, then ranks them and call the one with highest ranking.

However for example

type var;
void func(type);
void func(tpye&&);
func(var);

Both of the func methods have the same ranking. They are all exact match. No promotion or implicit type cast or anything else is needed. The same case for your question. So you may want to change

int addVertex(glm::vec3 p);

to

int addVertex(const glm::vec3& p);

because you're not aiming to change it. Some more about overload resolution and rvalue reference overload resolution, http://www.dcs.bbk.ac.uk/~roger/cpp/week20.htm, http://yapb-soc.blogspot.com/2015/01/rvalue-references-and-function.html

这篇关于具有重载r值参考函数的歧义调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆