【问题标题】:Ambiguous metafunction or undefined type模棱两可的元函数或未定义的类型
【发布时间】:2012-05-16 08:04:12
【问题描述】:

我是元函数的新手。我想编写一个函数,将复合类型中某种类型的所有匹配项替换为其他类型。例如: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&lt;void *, void *, int&gt; 时,编译器无法决定在这种情况下是使用replace&lt;C, C, Y&gt; 还是replace&lt;C *, X, Y&gt;,所以编译失败。

接下来我尝试在基函数中剥离指针:

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

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

【问题讨论】:

  • 让我看看我是否理解目标,给定两种模式,从差异中提取 const-volatile 和指针的差异并将其应用于第三个参数的类型?我不太确定问题是否明确,例如replace&lt;const void, void, const int&gt;::type 的输出是什么? replace&lt;void,int,double&gt;呢?
  • @David Rodríguez - dribeas:问题出自《C++ 模板元编程:Boost 及其他领域的概念、工具和技术》一书中的练习。也许我没有正确表达。练习内容为:Write a ternary metafunction replace_type&lt;c,x,y&gt; that takes an arbitrary compound type c as its first parameter, and replaces all occurences of a type x within c with y.
  • 我必须承认,书中的描述 together 和示例胜过原始问题

标签: c++ boost metaprogramming template-meta-programming boost-mpl


【解决方案1】:

这是一个总体思路:

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

【讨论】:

  • 模板中的模板,看起来很整洁(我永远不会想出那个!)。我会稍微尝试一下这个概念,一旦我觉得我理解了它就会回复你。谢谢
  • 我添加了一个小的概括,以便您编写专业。我目前正在努力正确处理数组(例如,指向 volatile 字符的常量指针数组)。
  • 到目前为止,效果真的很好!这是一个如此强大的概念。谢谢!
  • @nijansen:我添加了一个稍微改进的版本。
【解决方案2】:

与您尝试过的类似:

#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 的答案看起来好多了 :)

【讨论】:

    【解决方案3】:

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

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

    直播代码example results

    或者更多:

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

    直播代码results

    【讨论】:

      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      相关资源
      最近更新 更多