Color (int, int, int) vs Color (float, float, float) ambiguous call

How to resolve an ambiguous call between the two in C ++?

Color(int, int, int)
Color(float, float, float)

This is ambiguous when the values ​​are hardcoded, i.e. Color(1, 2, 3)and when they are variables Color(r, g, b). Why did the compiler not allow according to the data type? In variable form?

EDIT: Sorry, too much C ++ makes me forget about other languages. And there is not much "full code."

float x, y, z;
int r, g, b;
Color(1, 2, 3); // ambiguous
Color(1.0, 2.0, 3.0); // ambiguous
Color(r, g, b); // ambiguous  <--- this one is a real pain
Color((int)r, (int)g, (int)b); // ambiguous
Color(x, y, z); //OK
Color(1u, 2u, 3u); //OK
Color(1.0f, 2.0f, 3.0f); //OK
+3
source share
4 answers

The problem is that you announced

Color(unsigned, unsigned, unsigned);
Color(float, float, float);

.. float, unsigned. (, int double), - , , ( , ). , :

Color(int, int, int);
Color(double, double, double);

, .

+7

- double, float. , double, float, , . , , , :

Color(unsigned int, unsigned int, unsigned int);
Color(float, float, float);

, , Color(int, int, int), , . , :

Color(int, int, int);
Color(double, double, double);

, , :

Color((unsigned int)r, (unsigned int)g, (unsigned int)b);
+1

, , , , # ++.

. #:
Color((int)r, (int)g, (int)b)

Color((float)r, (float)g, (float)b)

0

, Color(r,g,b), , Color(int, int, int), Color(unsigned int, unsigned int, unsigned int). , .

0

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


All Articles