gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:05:15 24 4
gpt4 key购买 nike

我是元函数的新手。我想编写一个函数,将复合类型中特定类型的所有匹配项替换为其他类型。例如: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来自基本功能。

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

最佳答案

这是一个总体思路:

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

关于c++ - 不明确的元函数或未定义的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10488440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com