I have a class which has a template member functions and the private members needs to be put in the implementation class by pimpl idiom approach.
Foo.hpp
#include <iostream>
class Foo
{
public:
private:
class FooImpl;
std::unique_ptr<FooImpl> impl_;
public:
Foo() = default;
template <class F, class... Args>
auto SomeFunction ( F &&f, Args &&...args) -> boost::unique_future<decltype (f(args...))>;
};
Foo.cpp
#include <Foo.hpp>
#include <FooImpl.hpp>
template <class... Args>
template <class F, class... Args>
auto SomeFunction ( F &&f, Args &&...args) -> boost::unique_future<decltype (f(args...))>
{
impl->SomeFunction(std::forward<F>(f),std::forward<Args>(args)...);
}
FooImpl.hpp
#include <Foo.hpp>
class Foo::FooImpl
{
public:
FooImpl() = default;
template <class F, class... Args>
auto SomeFunction ( F &&f, Args &&...args) -> boost::unique_future<decltype (f(args...))>;
private:
int dummy;
};
FooImpl.cpp
#include <FooImpl.hpp>
template <class... Args>
template <class F, class... Args>
auto SomeFunction ( F &&f, Args &&...args) -> boost::unique_future<decltype (f(args...))>
{
//Do Something...
}
I have tried the following approaches but nothing worked in my case:-
- With virtual functions. This didn't work because virtual functions cannot be templated.
- Explicitly instantiate all the template instances like
template class Foo<int>;
template class Foo<float>;
This won't suit in my case because any type can come in my function. I cannot explicitly instantiate all types.
Is there other approaches? Is it possible to implement pimpl idiom with template functions? Or is there any other alternative to pimpl idiom?