I want to do the following with a specialization in attributes.
The following code overload IIwill never be used.
Someone said that T1can not be deduced in overload II.
How to fix it?
I used the C ++ shell to compile code with C ++ 14.
#include <iostream>
#include <type_traits>
using namespace std;
class A;
template <typename T>
struct is_A : false_type {};
template <> struct is_A<A> : true_type {};
template <typename T>
struct is_int : false_type {};
template <> struct is_int<int> : true_type {};
template <> struct is_int<long> : true_type {};
class A{
public:
int val;
void print(void){
std::cout << val << std::endl;
}
template <typename T1>
enable_if_t<is_int<T1>::value,void>
operator=(const T1 & input){
val = 2*input;
}
template <typename T1>
enable_if_t<is_A<T1>::value,void>
operator=(const T1 & Bb){
val = 5*Bb.val;
}
};
int main(void){
A Aa;
A Bb;
int in_a = 3;
Aa = in_a;
Bb = Aa;
Aa.print();
Bb.print();
}
source
share