Function with two typenames

Is it possible to handle a function that deals with two typenames T and U to let it return a U value?

In practice I should have something like T = vector<int> and U = vector<double>. The function should return a vector<double> value.

I have written a general template:
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
template <typename T,typename U>
class mix
{
private:
    vector<T> vt;
    vector<U> vu;
public:
    vector<U> f_mix(vector<T>& vt, vector<U>& vu);
    vector<T> get_v1() const {return vt;}
    vector<U> get_v2() const {return vu;}
};

template <typename T, typename U>
vector<U> mix<T,U>::f_mix(vector<T>& vt, vector<U>& vu)
{
    if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
    vector<U> vu1;
    vu1.reserve(vt.size());
    for (int i = 0; i < vt.size(); ++i)
    {
        vu1 = vt[i] * vu[i];
    }
    
    return vu1;
}


Then specialized:
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
template<>   // Specialization with both T and U specialized ---> Template typenames empty "<>" because
            // All specialized
class mix<vector<int>,vector<double>>        // Specialization
{
    vector<double> f_mix(vector<int>& vt, vector<double>& vu);
    vector<double> operator=(vector<int>& v1);
};

vector<double> mix<vector<int>,vector<double>>::operator=(vector<int>& v1)
{
    return v1;   // !! ERROR !!
}

vector<double> mix<vector<int>,vector<double>>::f_mix(vector<int>& vt, vector<double>& vu)
{
    if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
    vector<double> vu1;
    vu1.reserve(vt.size());
    for (int i = 0; i < vt.size(); ++i)
    {
        vu1 = vt[i] * vu[i];   // !! ERROR !!
    }
    
    return vu1;
}


But actually it tells me that there is no overload operator= to handle this. I have tried to overload operator but with any result. Can I do this or am I wrong?
Last edited on
vu1 is a vector. You're trying to assign a single numerical value (the result of vt[i] * vu[i]) to a vector.

Presumably, you meant:

vu1[i] = vt[i] * vu[i];
Thanks MikeyBoy, you are right. I wrote it and I missed this stupid mistake. Thanks!
No problem! Sometimes it's easy to miss the simplest of things :)
Wouldn't overloading the functions be simpler?

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <vector>
#include <string>
#include <type_traits>

template < typename T, typename U > auto mix( const std::vector<T>& a, const std::vector<U>& b )
{
    using common_type = typename std::common_type<T,U>::type ;
    std::vector<common_type> result ;

    auto iter_a = a.begin() ;
    for( auto iter_b = b.begin() ; iter_a != a.end() && iter_b != b.end() ; ++iter_a, ++iter_b )
        result.insert( result.end(), { common_type(*iter_a), common_type(*iter_b) } ) ;

    return result ;
}

auto mix( const std::vector<int>& a, const std::vector<double>& b )
{
    std::vector<int> result ;

    auto iter_a = a.begin() ;
    for( auto iter_b = b.begin() ; iter_a != a.end() && iter_b != b.end() ; ++iter_a, ++iter_b )
        result.insert( result.end(), { int( *iter_a + *iter_b ), int( *iter_a - *iter_b ) } ) ;

    return result ;
}

auto mix( const std::vector<double>& a, const std::vector<int>& b ) { return mix(b,a) ; }

std::string mix( const std::string& a, const std::string& b )
{
    std::string result ;

    auto iter_a = a.begin() ;
    for( auto iter_b = b.begin() ; iter_a != a.end() && iter_b != b.end() ; ++iter_a, ++iter_b )
        result.append( { *iter_a, *iter_b } ) ;

    return result ;
}

std::string mix( const char* a, const char* b ) { return mix( std::string(a), b ) ; }

template < typename T, typename U, typename V >
auto mix( const T& a, const U& b, const V& c ) { return mix( mix(a,b), c ) ; }

int main()
{
   const std::vector<double> a { 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 7.8 } ;
   const std::vector<int> b { 111, 222, 333, 444, 555, 666, 777, 888, 999 } ;
   const std::vector<char> c { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' } ;

   for( auto v : mix( a, b, c ) ) std::cout << v << ' ' ;
   std::cout << '\n' ;

   std::cout << mix( "hello!", "world!!", ".............." ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/c875914b97b0d1e8
Topic archived. No new replies allowed.