Feb 23, 2017 at 3:41pm UTC
I have the following function:
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
template <bool V>
struct conditional_assignment{};
template <>
struct conditional_assignment<false >{
template <typename T1, typename T2>
static void func(T1 &dst, const T2& src){
}
};
template <>
struct conditional_assignment<true >{
template <typename T1, typename T2>
static void func(T1 &dst, const T2& src){
dst = src;
}
};
template <typename T>
void f(const T &ip){
constexpr bool is_ip4 = IpAddress_to_protocol<T>::value == AF_INET;
constexpr bool is_ip6 = IpAddress_to_protocol<T>::value == AF_INET6;
if (is_ip4){
SomeType4 hop;
//...
conditional_assignment<is_ip4>::func(hop, ip);
//...
}else if (is_ip6){
SomeType6 hop;
//...
conditional_assignment<is_ip6>::func(hop, ip);
//...
}
}
I'd like to get rid of conditional_assignment, in favor of something a bit more legible. What could I do here?
PS: To be clear, T may be equal to IpAddress4 or IpAddress6, which have, respectively, implicit conversions to SomeType4 and SomeType6, but not to SomeType6 and SomeType4.
Last edited on Feb 23, 2017 at 3:44pm UTC
Feb 23, 2017 at 3:59pm UTC
I would still need two conditional_assignment overloads. I don't want to call a function just to maybe do an assignment.