I am currently trying to get the following to compile:
class foo {
};
class bar {
public:
const foo & to_foo() const {
return f;
}
foo & to_foo() {
return f;
}
private:
foo f;
};
template< typename T, typename Enable = void >
class convert {};
template< typename T >
struct convert< T, typename std::enable_if< std::is_member_function_pointer< decltype( &T::to_foo ) >::value >::type > {
static const foo & call1( const bar & b ) {
return b.to_foo();
}
static foo & call2( bar & b ) {
return b.to_foo();
}
};
However the specialisation get’s confused by the presence of two possible to_foo() members, so it will choose the default case. As soon as I remove one of the to_foo() members, it works, but then one of the callX() methods fails because it does not match the constness.
Is there any way to detect this function in this case?
EDIT:
Here is an example on ideone: http://ideone.com/E6saX
When one of the the methods is removed, it works just fine: http://ideone.com/iBKoN
It is still a bit unclear to me what you are trying to achieve. I will suppose that the target type (
foo) is fixed and we are not attempting to create a full bridge system.In this case, we can ditch the structure and just rely on overload selection.
Works just fine, as far as the actual translation goes. No template involved.
Now the question might be how to actually detect whether this conversion is possible or not. In this case, we need to use SFINAE to avoid a hard-error while attempting the conversion.
Note: successfully compiled on Clang 3.0 (with the work around) and gcc 4.5.1.