So you want TypeB<T>
to use the user defined conversions of T
?
Create a template operator U
that uses SFINAE to examine the conversion operators of T
and accept when U
is a type T
has an operator U
for.
An insufficient, yet easy, way is std::is_convertible
-- address of T::operator U
is probably better.
This will require C++11 features to do reasonably, because you'll want to use enable_if
in a default template parameter.
This is a sketchy implementation: I don't cover the target type having a constructor that takes the source type.
#include <utility>
#include <type_traits>
#include <iostream>
struct A {
operator int() { return 7; }
};
template<typename T>
struct unevaluated: std::true_type {};
template<typename T, typename U, typename=void>
struct has_user_defined_conversion:std::false_type {};
template<typename T, typename U>
struct has_user_defined_conversion<T, U,
typename std::enable_if< unevaluated<
decltype(
&T::operator U
)
>::value >::type
>: std::true_type {};
template<typename T>
struct fake {
T t;
template<typename U,
typename=typename std::enable_if<has_user_defined_conversion<T,U>::value>::type
>
operator U() { return t; }
};
int main() {
int x = fake<A>();
std::cout << x << "\n";
}