SFINAE:
template<bool isConst>
struct Interface
{
template<
bool Cond = isConst
, typename std::enable_if<!Cond, int>::type = 0
>
TypeA&
GetXpo3Container() const
{
return config_.type_a_member_;
}
template<
bool Cond = isConst
, typename std::enable_if<Cond, int>::type = 0
>
TypeA const&
GetXpo3Container() const
{
return config_.type_a_member_;
}
};
, , typename std::enable_if<Cond, int>::type - std::enable_if<isConst, int>::type , , -error, , SFINAE.
However, the default setting means that someone can do this, for example. Interface<true> f; TypeA& ref = f.GetXpo3Container<false>();. If you want to avoid this (for example, you do not trust users to not abuse the unspecified bits of your interface), here is another way to make the dependent element typeof std::enable_ifmore suitable, but somewhat more mysterious:
template<typename T, T Value, typename>
struct depend_on_c {
static constexpr T value = Value;
};
template<
typename Dummy = void
, typename std::enable_if<
depend_on_c<bool, isConst, Dummy>::value
, int
>::type = 0
>
source
share