3

私はメタ関数に不慣れです。複合型の特定の型のすべての一致を他の型に置き換える関数を作成したいと思います。例:replace<void *, void, int>::typeである必要がありint *、でreplace<void, void, int>::typeある必要がありますint

これまでのところ、基本的に2つの異なるアプローチで失敗しました。

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

コードに続いて、もう1つのスペシャライゼーションを追加してみませんか

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 に答える