J
James
I am trying to create a wrapper template that can forward parameters from
the wrapper constructor to the wrapped object constructor. I have been
having some trouble, and the only way I can even think of getting it work is
to use a boatload of overloads. Here is some sample code:
#include <iostream>
struct fubar
{
fubar()
{
std::cout << "("
<< this
<< ")->fubar::fubar()"
<< std::endl;
}
~fubar()
{
std::cout << "("
<< this
<< ")->fubar::~fubar()"
<< std::endl;
}
void display()
{
std::cout << "("
<< this
<< ")->fubar::display()"
<< std::endl;
}
};
struct foo
{
fubar& m_fubar;
foo(fubar& fubar_, int x)
: m_fubar(fubar_)
{
std::cout << "("
<< this
<< ")->foo::foo()"
<< std::endl;
}
~foo()
{
std::cout << "("
<< this
<< ")->foo::~foo()"
<< std::endl;
}
void display_fubar()
{
m_fubar.display();
}
};
template<typename T>
struct wrapper : public T
{
template<typename P1>
wrapper(P1& p1)
: T(p1)
{
}
template<typename P1>
wrapper(P1 const& p1)
: T(p1)
{
}
template<typename P1, typename P2>
wrapper(P1& p1, P2& p2)
: T(p1, p2)
{
}
template<typename P1, typename P2>
wrapper(P1 const& p1, P2& p2)
: T(p1, p2)
{
}
template<typename P1, typename P2>
wrapper(P1& p1, P2 const& p2)
: T(p1, p2)
{
}
template<typename P1, typename P2>
wrapper(P1 const& p1, P2 const& p2)
: T(p1, p2)
{
}
};
int
main()
{
{
fubar fb;
wrapper<foo> f(fb, 5);
f.display_fubar();
}
return 0;
}
Notice all of the constructor overloads in the wrapper template... Are those
even legal? Can they get me into trouble? Will there be any ambiguity
problems?
One problem I can see... The number of overloads can get very high when the
number of parameters increases. I guess I can create a little program to
automatically generate all of them. Would that even be feasible?
Thanks!
the wrapper constructor to the wrapped object constructor. I have been
having some trouble, and the only way I can even think of getting it work is
to use a boatload of overloads. Here is some sample code:
#include <iostream>
struct fubar
{
fubar()
{
std::cout << "("
<< this
<< ")->fubar::fubar()"
<< std::endl;
}
~fubar()
{
std::cout << "("
<< this
<< ")->fubar::~fubar()"
<< std::endl;
}
void display()
{
std::cout << "("
<< this
<< ")->fubar::display()"
<< std::endl;
}
};
struct foo
{
fubar& m_fubar;
foo(fubar& fubar_, int x)
: m_fubar(fubar_)
{
std::cout << "("
<< this
<< ")->foo::foo()"
<< std::endl;
}
~foo()
{
std::cout << "("
<< this
<< ")->foo::~foo()"
<< std::endl;
}
void display_fubar()
{
m_fubar.display();
}
};
template<typename T>
struct wrapper : public T
{
template<typename P1>
wrapper(P1& p1)
: T(p1)
{
}
template<typename P1>
wrapper(P1 const& p1)
: T(p1)
{
}
template<typename P1, typename P2>
wrapper(P1& p1, P2& p2)
: T(p1, p2)
{
}
template<typename P1, typename P2>
wrapper(P1 const& p1, P2& p2)
: T(p1, p2)
{
}
template<typename P1, typename P2>
wrapper(P1& p1, P2 const& p2)
: T(p1, p2)
{
}
template<typename P1, typename P2>
wrapper(P1 const& p1, P2 const& p2)
: T(p1, p2)
{
}
};
int
main()
{
{
fubar fb;
wrapper<foo> f(fb, 5);
f.display_fubar();
}
return 0;
}
Notice all of the constructor overloads in the wrapper template... Are those
even legal? Can they get me into trouble? Will there be any ambiguity
problems?
One problem I can see... The number of overloads can get very high when the
number of parameters increases. I guess I can create a little program to
automatically generate all of them. Would that even be feasible?
Thanks!