I'm new to metafunds. I want to write a function that replaces all matches of a particular type in a composite type with another type. In the example: replace<void *, void, int>::typeshould be int *, replace<void, void, int>::typeshould be int, etc.
I basically failed with two different approaches:
template
<
typename C,
typename X,
typename Y
>
struct replace
{
typedef C type;
};
template
<
typename C,
typename Y
>
struct replace<C, C, Y>
{
typedef Y type;
};
template
<
typename C,
typename X,
typename Y
>
struct replace<C *, X, Y>
{
typedef typename replace<C, X, Y>::type * type;
};
I thought it was pretty straightforward, but I found that if you try replace<void *, void *, int>the compiler can not decide whether to use in this case, replace<C, C, Y>or replace<C *, X, Y>so the compilation is not performed.
The next thing I tried is to skip pointers in the base function already:
template
<
typename C,
typename X,
typename Y
>
struct replace
{
typedef typename boost::conditional
<
boost::is_pointer<C>::value,
typename replace
<
typename boost::remove_pointer<C>::type,
X, Y
>::type *,
C
>::type
type;
};
... , , type, -, , typedef .
. ββ?