简体   繁体   English

C++ 接受错误的返回类型

[英]C++ accepts wrong return-type

I have the following code, which, in my opinion, should not compile: (In a class Vector ( not the std::vector , but a Vector that I defined myself)):我有以下代码,在我看来,不应该编译:(在class Vector中(不是std::vector ,而是我自己定义的Vector )):

Vector operator*(double factor)
{
  // some element-wise multiplication here
  return factor;
}

I am defining this operator to return a Vector , but inside the method i return a double .我将这个运算符定义为返回一个Vector ,但在方法内部我返回一个double

I also have not defined any implicit conversion operators inside the class.我也没有在 class 中定义任何隐式转换运算符

I would think that this is clearly not valid, but gcc happily compiles this, without any complaint at all.我认为这显然是无效的,但是 gcc 很高兴地编译了这个,完全没有任何抱怨。

My first reflex is to file a defect-report to gcc, but I thought I would ask here before..我的第一反应是向 gcc 提交缺陷报告,但我想我之前会在这里问..

Am I missing something here that would explain why this code is considered valid by the compiler?我是否在这里遗漏了一些可以解释为什么编译器认为此代码有效的内容?

(In case it's relevant, c++-standard is set to c++17) (如果相关,c++-standard 设置为 c++17)

Some more Info on the Vector class: (namely, I include all declared constructors and operators, as other, named functions should not contribute to this issue):有关Vector class 的更多信息:(即,我包括所有声明的构造函数和运算符,因为其他命名函数不应导致此问题):

class Vector {
public:
  Vector(std::vector<double> values);
  Vector(size_t dimension, double value);
  Vector(size_t dimension);

  void operator*=(double factor);
  void operator*=(Vector const& factor);

  friend Vector operator-(Vector const& a, Vector const& b);
  friend Vector operator+(Vector const& a, Vector const& b);
  friend Vector operator*(Vector const& a, Vector const& b);

  Vector operator*(double factor);
  double& operator[](size_t i);

  friend std::ostream& operator<<(std::ostream& stream, Vector const& v);
}

Basing this answer on received comments:根据收到的评论给出这个答案:

Non-explicit conversion-operators are not the only way in which a return-value that initially has the wrong type may "become" the type needed for the function-signature.非显式转换运算符不是最初具有错误类型的返回值可能“成为”函数签名所需类型的唯一方式。 The constructor构造函数

Vector(size_t dimension);

Takes one numerical value as an argument, which itself is not a double , but a long unsigned int , but a double can be converted into that.将一个数值作为参数,它本身不是double ,而是long unsigned int ,但可以将double转换为该参数。

So what happens is that the double factor is first converted into a long unsigned int and then an implicit construction of Vector , using the constructor Vector(size_t dimension);所以发生的情况是, double factor首先被转换为一个long unsigned int ,然后是Vector的隐式构造,使用构造函数Vector(size_t dimension); happens.发生。 The object that is the result of this construction is then returned.然后返回作为此构造结果的 object。

Therefore, this is valid code in the sense of not breaking language-rules, the compiler is definitely not doing anything wrong here.因此,在不违反语言规则的意义上,这是有效的代码,编译器在这里绝对没有做错任何事情。

If this behaviour is not wanted, the constructor in question should be marked explicit like this:如果不希望出现这种行为,则应将相关构造函数标记为explicit ,如下所示:

explicit Vector(size_t dimension);

If there were multiple constructors taking one argument of a type into which a double can be converted, or whose first arguments fulfill that requirement with all other arguments being optional, for example:如果有多个构造函数采用一个可以将double类型转换为的参数,或者其第一个 arguments 满足该要求,而所有其他 arguments 都是可选的,例如:

Vector(int a, char b = 'c');

they would have to be marked explicit as well, but in that case it is likely that the compiler would complain, because it could not deduce which constructor to call.它们也必须被标记为explicit ,但在这种情况下,编译器很可能会抱怨,因为它无法推断出要调用哪个构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM