Problem with generic struct

I need to return type that is implicit conversion of type A and type B.
Fox example :
double a + int b → implicit conversion is double
how can I do that, because C cannot be determined like this

1
2
3
4
5
6
7
8
9
10
  template<typename A>
struct  series { 

A member;
}

template<typename A,typename B,typename C>
series <C> & operator + (A first, const B& second) {
return first.member+second.member;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <type_traits>

template < typename T > struct series { T member ; };

template < typename A, typename B >
constexpr auto operator + ( const A& first, const B& second )
{
    using common_type = typename std::common_type< decltype(A::member), decltype(B::member) >::type ;
    return series<common_type>{ first.member + second.member };
}

int main()
{
    const struct X { char member = 50 ; } xx;
    const struct Y { double member = 5.5 ; } yy;

    const auto z = xx + yy ; // series<double>{ char(50) + 5.5 } ;
    static_assert( std::is_same< const series<double>, decltype(z) >::value ) ;
    std::cout << xx.member + yy.member << ' ' << z.member << '\n' ; // 55.5 55.5
}

http://coliru.stacked-crooked.com/a/de244ebbfca84f7d
1
2
3
4
5
template < typename A, typename B >
constexpr auto operator+(const A& first, const B& second) ->series<decltype(first.member + second.member)> {
	return series<decltype(first.member + second.member)>(first.member + second.member);
}

Topic archived. No new replies allowed.