Ambiguous metafunction or undefined type

∥☆過路亽.° 提交于 2019-12-10 10:22:25

问题


I am new to metafunctions. I want to write a function that replaces all matches of a certain type in a compound type with some other type. In example: replace<void *, void, int>::type should be int *, replace<void, void, int>::type should be int, etc.

I basically failed with two different approaches so far:

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

This seemed pretty straight forward to me, but I discovered that when I try replace<void *, void *, int>, the compiler cannot decide whether to use replace<C, C, Y> or replace<C *, X, Y> in that case, so compilation fails.

The next thing I tried is to strip 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;
};

...and this is when I found out that I cannot do that either, because type is apparently not defined at that point, so I cannot do recursive typedef from the base function.

Now I am out of ideas. How would you solve such a problem?


回答1:


Here's a general idea:

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: Here's a somewhat more complete set:

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



回答2:


Something along the lines of what you tried:

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

Kerrek's answer looks so much better :)




回答3:


Following your code, why not add one more specialization

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

live code example results

Or further more:

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

live code results



来源:https://stackoverflow.com/questions/10488440/ambiguous-metafunction-or-undefined-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!