I'm using pimpl pattern in my code, so for each class X
, there will be a corresponding impl class XImpl
Say we have A
, AImpl
, B
, BImpl
, C
, CImpl
, where A
,B
,C
are similar class, and I would like to write some template function for them.
template <typename T, typename TImpl>
std::shared_ptr<const T> GetObjectById(int id);
This function is to query Object by its id(type could be A
or B
or C
). In the function definition, I do need to use both T
and TImpl
.
Users will use this function in this way:
auto a = GetObjectById<A, AImpl>(1);
auto b = GetObjectById<B, BImpl>(2);
auto c = GetObjectById<C, CImpl>(3);
And as you can see, for each type T
, TImpl
is always bind to it. So I want to simplify its usage to
auto a = GetObjectById<A>(1);
auto b = GetObjectById<B>(2);
auto c = GetObjectById<C>(3);
More importantly, this can hide AImpl
from user side.
Is there anyway to bind second argument to the first argument? i.e., the second argument doesn't participate in argument deduction, it's specified by some mapping relationship I defined.
Do you mean something as follows ?
template <typename T, typename TImpl = typename T::impl>
std::shared_ptr<const T> GetObjectById(int id);