Type Mismatch between the same Type

Dear love Community,
excause me for this little stupid Question.

The Situation:
I have a File x.h:

typedef std::pair<float, float> Bund;

I have new a y.h:

#include "Data.h"
class A
{
public:
Bund n;

and new in Data.cpp:

int x1 = SpinDurationMin->GetValue();
int x2 = SpinDurationMax->GetValue();
Bund m (float(x1), float(x2));
n = m;

I get a Error of Type mismatch:
template argument deduction/substitution failed:
TrackParameter.cpp:70:17: note: mismatched types ‘std::pair<_T1, _T2>’ and ‘Bund(float, float)’ {aka ‘std::pair<float, float>(float, float)’}


Last edited on
What's m? Is m a function?
Last edited on
Cannot replicate the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <utility>


using Bund = std::pair<float, float> ;


class A {
public:
    Bund n;
};


int main()
{
    int x1 = 1;
    int x2 = 2;
    Bund m (static_cast<float>(x1), static_cast<float>(x2));
    A a;
    a.n = m;
    std::cout << "first: " << a.n.first << "; second: " << a.n.second << '\n';
}


Output:
first: 1; second: 2

Oh, yes, I can:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <utility>


using Bund = std::pair<float, float> ;


class A {
public:
    Bund n;
};


int main()
{
    int x1 = 1;
    int x2 = 2;
    Bund m (float(x1), float(x2));
    A a;
    a.n = m;
    std::cout << "first: " << a.n.first << "; second: " << a.n.second << '\n';
}


Output:
error: no match for 'operator='
 (operand types are 'Bund' {aka 'std::pair<float, float>'}
 and 'Bund(float, float)' {aka 'std::pair<float, float>(float, float)'})
   20 |     a.n = m;
      |           ^


It seems it can’t understand (float(x1), float(x2)) is an attempt to convert int to float…
But default conversion (into larger type) works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <utility>


using Bund = std::pair<float, float> ;


class A {
public:
    Bund n;
};


int main()
{
    int x1 = 1;
    int x2 = 2;
    Bund m (x1, x2);
    A a;
    a.n = m;
    std::cout << "first: " << a.n.first << "; second: " << a.n.second << '\n';
}

Funny :)
However the uniform initializer seems to solve every problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <utility>


using Bund = std::pair<float, float> ;


class A {
public:
    Bund n;
};


int main()
{
    int x1 = 1;
    int x2 = 2;
    Bund m { float(x1), float(x2) };
    A a;
    a.n = m;
    std::cout << "first: " << a.n.first << "; second: " << a.n.second << '\n';
}


Output:
first: 1; second: 2


I’d go with the explicit cast (static_cast<>) AND the uniform initializer. IMHO.
But we need to wait for some syntax guru: there’s quite a few of them on this forum.
The general syntax parsing rule here is that if something can be a function declaration, it is a function declaration.

To the compiler,
Bund m (float(x1), float(x2));
means "Declare a function named m that takes in two floats (parameters x1, x2) and returns a Bund".
The parentheses around (x1) and (x2) are then seen as unnecessary, but still allowed.
Last edited on
std::make_pair can absolve a lot of sins when dealing with pairs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <utility>
#include <typeinfo>

using Bund = std::pair<float, float>;

class A
{
public:
   Bund n;
};

int main()
{
   int x1 = 1;
   int x2 = 2;

   Bund m = std::make_pair(x1, x2);

   A a;

   a.n = m;

   std::cout << "first: " << a.n.first << "; second: " << a.n.second << "\n\n";

   std::cout << typeid(m).name() << ", " << typeid(m.first).name() << ", " << typeid(m.second).name() << '\n';
}

first: 1; second: 2

struct std::pair<float,float>, float, float
Last edited on
Topic archived. No new replies allowed.