C ++ overload with one const parameter

Why the following is not valid in C++

 #include <iostream> class Sample { public: void Method(char x); void Method(char const x); }; void Sample::Method(char x) { char y = x; } void Sample::Method(char const x) { char y = x; } int main() { Sample s; return 0; } 
+6
source share
7 answers

In fact, he does not answer why, but he is defined by the standard, ยง1.3.10

Information about the function involved in overload resolution (13.3): types of its parameters and, if the function is a member of the class, cv-qualifiers (if any) for the function itself and the class in which the member function is declared.

This means that the cv argument qualifiers are ignored when overload resolution is enabled.

A similar (but not equivalent) link operation example:

 class Sample { public: void Method(char& x) {} void Method(const char& x) {} }; 

because there are different types, the first case is a reference to char , the second is a reference to const char (as opposed to a const reference to char ).

+5
source

Why is the following not allowed in C ++?
The reason is that the compiler gives you a compilation error:
Because they are ambiguous!

Why are these methods ambiguous?
Short answer: Because the C ++ standard says so.

What rationale for these overloaded methods is ambiguous?
The compiler does not know whether the caller wants to parse the value of the argument passed as a const or not, the compiler can not determine that from this information. p>

Note the emphasis on pass by value here, the argument is passed by value and therefore ambiguity. If the argument was passed by reference , then the compiler knows exactly how the caller should handle the argument, because then the actual object itself is passed, and therefore the compiler can choose the right overload.

The following example gives a clearer picture of this explanation.

Online example :

 class Sample { public: void Method(char &x){} void Method(char const x){} void Method(char const &x){} }; int main() { Sample s; return 0; } 
+6
source

When it comes to functional parameters, char and char const are the same data type.

+3
source

This is still controversial. When it calls with a character argument, one version copies the argument and says "OK, you can change the copy." Another will copy the argument and say "OK, you cannot change the copy." How should a compiler know if it can or cannot change a copy of something? It can do either just fine.

+3
source

because it is ambiguous when you pass like this

s.Method('x');

which version should you name?

+2
source

The standard states that these two declarations are equivalent (13.1.3):

Designations of parameters that differ only in the presence or absence of const and / or volatile are equivalent. That is, the type specifiers const and volatile for each type of parameter are ignored when determining which function is declared, defined, or called.

 typedef const int cInt; int f(int); int f(const int); // redeclaration of f(int) int f(int) { /* ... */ } // definiton of f(int) int f(cInt) { /* ... */ } // error: redefiniton of f(int) 
+2
source

http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Because const denotes this variable as having a set value that cannot be changed after the declaration. This is not a different data type.

0
source

Source: https://habr.com/ru/post/915661/


All Articles