用C ++编写类型类

时间:2018-06-14 20:15:07

标签: c++ templates metaprogramming typeclass template-meta-programming

我试图用类似的C ++编写类似于一组类型类的东西,而且我在如何安排模板签名方面苦苦挣扎,或者如果它甚至可以做我想做的事情要做。

要把它分解成最小的例子,说我有这个:

template<typename S, typename T>
struct Homomorphism {
    //Defined in specialization: static const T morph(const S&);
    static constexpr bool is_instance = false;
    using src  = S;
    using dest = T;
};

template<typename S, typename T>
struct Monomorphism : Homomorphism<S, T> {
    //Defined in specialization: static const T morph(const &S);
    static constexpr bool is_instance = false;
    using src  = S;
    using dest = T;
};

我对我的程序中的数据类型有这些类(和其他态射)的特化。

我现在要做的是编写一个结构模板,它将采用两个同态或两个同态,并将它们组合成分别生成一个新的同态或单态结构,例如:

template<typename S, typename T, typename U,
         typename HST = Homomorphism<S, T>,
         typename HTU = Homomorphism<T, U>,
         typename HSU = Homomorphism<S, U> >
struct CompositionMorphism : HSU {
    static const U morph(const S &s) {
        return HTU::morph(HST::morph(s));
    }
    static constexpr bool is_instance = true;
    using src  = S;
    using dest = U;
}

这实际上适用于通过以下方式编写Homomorphism的特殊实例:

CompositionMorphism<Class1, Class2, Class3>::morph(class1Instance);

当我有:

struct Homomorphism<Class1, Class2> {
    static const Class2 morph(const Class1 &c) {
        ...
    }
};

Homomorphism<Class2, Class3>类似。

但是,现在,我想写一下:

template<typename S, typename T, typename U,
        typename MST = Monomorphism<S, T>,
        typename MTU = Monomorphism<T, U>,
        typename MSU = Monomorphism<S, U> >
struct CompositionMorphism : MSU {
    static const U morph(const S &s) {
        return MTU::morph(MST::morph(s));
    }
    static constexpr bool is_instance = true;
    using src  = S;
    using dest = U;
};

但编译器抱怨CompositionMorphism的重复定义并不令人惊讶。

有没有办法用CompositionMorphismHomomorphism来编写Monomorphism及其专精,以便我可以执行以下操作:

template<> struct Homomorphism<Class1, Class2> { ... };
template<> struct Homomorphism<Class2, Class3> { ... };
CompositionMorphism<Class1, Class2, Class3>::morph(c1Instance);

或:

template<> struct Monomorphism<Class1, Class2> { ... };
template<> struct Monomorphism<Class2, Class3> { ... };
CompositionMorphism<Class1, Class2, Class3>::morph(c1Instance);

或:

template<> struct Monomorphism<Class1, Class2> { ... };
template<> struct Homomorphism<Class2, Class3> { ... };
CompositionMorphism<Class1, Class2, Class3>::morph(c1Instance);

让编译器根据我的态射层次结构选择最接近的CompositionMorphism专门化?

3 个答案:

答案 0 :(得分:1)

好的,有时候我需要多一点思考,但这可能就是你在寻找的东西:

#include <type_traits>
#include <cstdint>
#include <tuple>

template<typename S, typename T>
struct Homomorphism;

template<typename S, typename T>
struct Monomorphism;

class Class1{};
class Class2{};
class Class3{};

template<> struct Homomorphism<Class1, Class2> 
{ 
     static const Class2 morph(const Class1&); 
     static constexpr bool is_instance = true;#
};

template<> struct Homomorphism<Class2, Class3> 
{
    static const Class3 morph(const Class2&);
    static constexpr bool is_instance = true;
};

template<typename S, typename T>
struct Homomorphism {
    //Defined in specialization: static const T morph(const S&);
    static constexpr bool is_instance = false;
    using src  = S;
    using dest = T;
};

template<typename S, typename T>
struct Monomorphism : Homomorphism<S, T> {
    //Defined in specialization: static const T morph(const &S);
    static constexpr bool is_instance = false;
    using src  = S;
    using dest = T;
};


namespace details {
    template<typename T, typename U, std::enable_if_t<Homomorphism<T,U>::is_instance>* = nullptr>
    U morph (const T& t)
    {return  Homomorphism<T,U>::morph(t);}

    template<typename T, typename U,  std::enable_if_t<Monomorphism<T,U>::is_instance>* = nullptr>
    U morph (const T& t)
    {return  Monomorphism<T,U>::morph(t);}


 }

template <typename S, typename T, typename U>
class CompositionMorphism
{
public:
    static U morph (const S& s)  {return  details::morph<T,U>(details::morph<S,T>(s));}
    static constexpr bool is_instance = true;
};


 int main(int, char**)
{
    Class1 c1Instance;
    CompositionMorphism<Class1, Class2, Class3>::morph(c1Instance);
    std::ignore = d;
}

您可能想要手动创建组合的Homo / Mono态射,如下所示:

template <> class Monomorphism<Class1,Class3> : public CompositionMorphism<Class1, Class2, Class3> {};

然后它们可以被CompositionMorphism自动重用。

答案 1 :(得分:1)

您可以尝试使用Homomorphism功能编写模板,根据SFINAE选择Monomorphismmorph

template <typename S, typename T, typename = void>
struct SelectMorphism {
    using type = Homomorphism<S, T>;
};

template <typename S, typename T>
struct SelectMorphism<S, T, std::enable_if_t<std::is_same_v<decltype(Monomorphism<S, T>::morph(std::declval<S>())), const T>>> {
    using type = Monomorphism<S, T>;
};

这会检查Monomorphism<S, T>::morph(S)是否会返回T,如果是,请选择Monomorphism<S, T>。如果不是,SFINAE将失败并默认为Homomorphism<S, T>

然后我们更改CompositionMorphism以使用此模板

template<typename S, typename T, typename U,
         typename HST = typename SelectMorphism<S, T>::type,
         typename HTU = typename SelectMorphism<T, U>::type,
         typename HSU = typename SelectMorphism<S, U>::type >
struct CompositionMorphism : HSU {
    static const U morph(const S &s) {
        return HTU::morph(HST::morph(s));
    }
    static constexpr bool is_instance = true;
    using src  = S;
    using dest = U;
};

您可以看到这个完整工作示例的live demo here。它需要c++17,但也可以为c++11编写(稍微详细一些)。

#include <iostream>

template<typename S, typename T>
struct Homomorphism {
    //Defined in specialization: static const T morph(const S&);
    static constexpr bool is_instance = false;
    using src  = S;
    using dest = T;
};

template<typename S, typename T>
struct Monomorphism : Homomorphism<S, T> {
    //Defined in specialization: static const T morph(const &S);
    static constexpr bool is_instance = false;
    using src  = S;
    using dest = T;
};

template <typename S, typename T, typename = void>
struct SelectMorphism {
    using type = Homomorphism<S, T>;
};

template <typename S, typename T>
struct SelectMorphism<S, T, std::enable_if_t<std::is_same_v<decltype(Monomorphism<S, T>::morph(std::declval<S>())), const T>>> {
    using type = Monomorphism<S, T>;
};

struct Class1 {};

struct Class2 {};

struct Class3 {};

template<>
struct Monomorphism<Class1, Class2> : Homomorphism<Class1, Class2> {
    static const Class2 morph(const Class1&) { std::cout << "Morphing in Mono<Class1, Class2>" << std::endl; return Class2{}; }
    static constexpr bool is_instance = false;
    using src  = Class1;
    using dest = Class2;
};

template<>
struct Homomorphism<Class2, Class3> {
    static const Class3 morph(const Class2&) { std::cout << "Morphing in Homo<Class2, Class3>" << std::endl; return Class3{}; }
    static constexpr bool is_instance = false;
    using src  = Class2;
    using dest = Class3;
};

template<typename S, typename T, typename U,
         typename HST = typename SelectMorphism<S, T>::type,
         typename HTU = typename SelectMorphism<T, U>::type,
         typename HSU = typename SelectMorphism<S, U>::type >
struct CompositionMorphism : HSU {
    static const U morph(const S &s) {
        return HTU::morph(HST::morph(s));
    }
    static constexpr bool is_instance = true;
    using src  = S;
    using dest = U;
};

int main ()
{
    CompositionMorphism<Class1, Class2, Class3>::morph(Class1{});
}

答案 2 :(得分:1)

正如Super所观察到的,如果只传递TUV,则编译器不知道选择Homomorphism还是{{1}的情况}}

所以我想你应该通过MonomorphismHomomorphism<T, U>Homomorphism<U, V>可以构建)或Homomorphism<T, V>Monomorphism<T, U>

如果你想强加两个Monomorphism<U, V> 两个Homomorphism(我的意思是:如果你想要MonomorphismMonomorphism一起排除{)你可以写下如下内容

Homomorphism

并按如下方式调用

template <typename, typename>
struct CompositionMorphism;

template <template <typename, typename> class C,
          typename S, typename T, typename U>
struct CompositionMorphism<C<S, T>, C<T, U>>
 {
   using comp = C<S, U>;

   static const U morph (const S & s)
    { return C<T, U>::morph(C<S, T>::morph(s)); }
 };

以下是完整的编译示例

   Homomorphism<int, long>        h0;
   Homomorphism<long, long long>  h1;
   Monomorphism<int, long>        m0;
   Monomorphism<long, long long>  m1;

   CompositionMorphism<decltype(h0), decltype(h1)>  h2;
   CompositionMorphism<decltype(m0), decltype(m1)>  m2;

   // compiler error
   //CompositionMorphism<decltype(h0), decltype(m1)>  hm;