3

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

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

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

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

4

3 回答 3

3

这是一个总体思路:

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;
};

测试:

#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;
}

印刷:

int
int*

编辑:这是一个更完整的集合:

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;
};
于 2012-05-07T20:28:49.730 回答
1

类似于您尝试的内容:

#include <typeinfo>
#include <type_traits>

template<typename C, typename X, typename Y>
struct replace {
private:

    typedef
        typename std::conditional <
            std::is_pointer<C>::value,
            typename std::remove_pointer<C>::type,
            C >::type
        strippedT;

    typedef
        typename std::conditional <
            std::is_same<strippedT, X>::value,
            Y,
            strippedT>::type
        replacedT;
public:

    typedef
        typename std::conditional <
            std::is_pointer<C>::value,
            replacedT*,
            replacedT >::type
        type;
};

int main()
{
    typedef replace<void*, void, int>::type T1;
    std::cout << typeid(T1).name() << '\n';  // prints Pi on my implementation

    typedef replace<void, void, int>::type T2;
    std::cout << typeid(T2).name();          // prints i
}

克雷克的答案看起来好多了:)

于 2012-05-07T20:48:36.083 回答
0

按照您的代码,为什么不再添加一个专业

template
    <
        typename C,
        typename Y
    >
struct replace<C*, C, Y>
{
    typedef Y* type;
};

实时代码示例结果

或者更多:

template<class T>
struct tag_t {using type=T;};

template <class C, class X, class Y>
struct replace {};

template <class C, class Y>
struct replace<C, C, Y>: tag_t<Y> {};

template <class C, class Y>
struct replace<C*, C, Y>: tag_t<Y*>{}; 

实时代码结果

于 2017-08-05T18:56:45.167 回答