Re: Tricks for complicated typedefs inside of make_ functions
On 6 Jul., 05:16, Jesse Perla <jessepe...@gmail.com> wrote:
I imagine that typedef templates would help this, but I wanted to see
if there were any immediate tricks I could use:
I am usng the make_ function idiom to create a templated class and
have very complicated typedefs that need to appear in many places.
Are there any ways to make this cleaner/less error prone? Here is a
vastly simplified example of the pattern.
template < class Type1, class Type2>
my_class< Type1, VeryComplicatedType<Type1, Type2>>
make_my_class(Type1 t1, VeryComplicatedType<Type1, Type2> ct)
{
return my_class<Type1, VeryComplicatedType<Type1, Type2>>(t1, ct);
}
Any way to get a typedef for VeryComplicatedType<Type1, Type2> ?
The C++03 a classical workaround to define a class
template (your mentioned "typedef template") that acts
as some kind of template alias, e.g.:
template <class T1, class T2>
struct Alias {
typedef VeryComplicatedType<T1, T2> type;
};
template <class Type1, class Type2>
my_class<Type1, typename Alias<Type1, Type2>::type>
make_my_class(Type1 t1, VeryComplicatedType<Type1, Type2> ct)
{
return my_class<Type1, typename Alias<Type1, Type2>::type>(t1,
ct);
}
is only half-baken here, because you cannot substitute
the second function parameter type by the alias,
because this would prevent an argument at this position
to be implicitly deducible.
Even though I dislike to say that, but you could consider
to use a locally valid macro:
#define MY_ALIAS(Type1, Type2) \
VeryComplicatedType<Type1, Type2>
template <class Type1, class Type2>
my_class<Type1, MY_ALIAS(Type1, Type2)>
make_my_class(Type1 t1, MY_ALIAS(Type1, Type2) ct)
{
return my_class<Type1, MY_ALIAS(Type1, Type2) >(t1, ct);
}
#undef MY_ALIAS
C++0x provides a real template alias and then your code
would look like this:
template <class T1, class T2>
using Alias = VeryComplicatedType<T1, T2>;
template <class Type1, class Type2>
my_class<Type1, Alias<Type1, Type2>>
make_my_class(Type1 t1, Alias<Type1, Type2> ct)
{
return my_class<Type1, Alias<Type1, Type2>>(t1, ct);
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]