不明确的元函数或未定义的类型

2024-04-29

我是元功能的新手。我想编写一个函数,将复合类型中某种类型的所有匹配项替换为其他类型。在示例中:replace<void *, void, int>::type应该int *, replace<void, void, int>::type应该int, etc.

到目前为止,我基本上用两种不同的方法失败了:

template
    <
        typename C, // Type to be searched
        typename X, // "Needle" that is searched for
        typename Y  // Replacing type
    >
struct replace
{
    typedef C type;
};

// If the type matches the search exactly, replace
template
    <
        typename C,
        typename Y
    >
struct replace<C, C, Y>
{
    typedef Y type;
};

// If the type is a pointer, strip it and call recursively
template
    <
        typename C,
        typename X,
        typename Y
    >
struct replace<C *, X, Y>
{
    typedef typename replace<C, X, Y>::type * type;
};

这对我来说似乎很简单,但我发现当我尝试时replace<void *, void *, int>,编译器无法决定是否使用replace<C, C, Y> or replace<C *, X, Y>在这种情况下,编译会失败。

我尝试的下一件事是已经删除基本函数中的指针:

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从基函数。

现在我没有主意了。你会如何解决这样的问题?


这是一个总体思路:

template <typename, typename> struct pattern;

template <typename T> struct pattern<T, T>
{
    template <typename U> struct rebind
    {
        typedef U other;
    };
};

template <typename A, typename B> struct pattern<A*, B>
{
    template <typename U> struct rebind
    {
        typedef typename pattern<A, B>::template rebind<U>::other * other;
    };
};

template <typename Haystack, typename Needle, typename New>
struct replace
{
    typedef typename pattern<Haystack, Needle>::template rebind<New>::other type;
};

Test:

#include <demangle.hpp>
#include <iostream>
int main()
{
    typedef replace<void, void, int>::type T1;
    typedef replace<void*, void, int>::type T2;

    std::cout << demangle<T1>() << std::endl;
    std::cout << demangle<T2>() << std::endl;
}

Prints:

int
int*

Edit:这是一个更完整的集合:

template <typename, typename> struct pattern;
template <typename, typename> struct pattern_aux;

template <typename A, typename B> struct pattern_aux
{
    template <typename U> struct rebind
    {
        typedef typename pattern<A, B>::template rebind<U>::other other;
    };
};

template <typename A, typename B, unsigned int N> struct pattern_aux<A[N], B>
{
    template <typename U> struct rebind
    {
        typedef typename pattern<A, B>::template rebind<U>::other other[N];
    };
};


template <typename A, typename B> struct pattern
{
    template <typename U> struct rebind
    {
        typedef typename pattern_aux<A, B>::template rebind<U>::other * other;
    };
};

template <typename T> struct pattern<T, T>
{
    template <typename U> struct rebind
    {
        typedef U other;
    };
};

template <typename A, typename B> struct pattern<A*, B>
{
    template <typename U> struct rebind
    {
        typedef typename pattern<A, B>::template rebind<U>::other * other;
    };
};

template <typename A, typename B> struct pattern<A const, B>
{
    template <typename U> struct rebind
    {
        typedef typename pattern_aux<A, B>::template rebind<U>::other const other;
    };
};

template <typename A, typename B> struct pattern<A volatile, B>
{
    template <typename U> struct rebind
    {
        typedef typename pattern_aux<A, B>::template rebind<U>::other volatile other;
    };
};

template <typename A, typename B> struct pattern<A const volatile, B>
{
    template <typename U> struct rebind
    {
        typedef typename pattern_aux<A, B>::template rebind<U>::other const volatile other;
    };
};

template <typename Haystack, typename Needle, typename New>
struct replace
{
    typedef typename pattern<Haystack, Needle>::template rebind<New>::other type;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不明确的元函数或未定义的类型 的相关文章

随机推荐