我正试图在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;
}这实际上适用于通过以下方法合成同态的专门实例:
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的定义是重复的。
是否有一种方法可以用CompositionMorphism和Monomorphism编写Homomorphism及其专门化,以便我能够执行诸如调用这样的操作:
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专门化?
发布于 2018-06-14 21:30:52
您可以尝试编写一个模板,根据Homomorphism函数上的SFINAE选择Monomorphism或morph。
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;
};您可以看到这个完整的工作示例的现场演示。它需要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{});
}发布于 2018-06-14 20:55:05
好吧,有时候我需要更多的思考,但这可能是你想要的:
#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自动重用。
发布于 2018-06-14 21:34:03
正如Super所观察到的,如果您只传递T、U和V,编译器就不知道是选择Homomorphism还是Monomorphism。
因此,我认为您应该通过Homomorphism<T, U>和Homomorphism<U, V> (可以构造Homomorphism<T, V>)或Monomorphism<T, U>和Monomorphism<U, V>
如果您想要强制执行两个Homomorphism 或 two Monomorphism (我的意思是:如果您想用Homomorphism排除Monomorphism toghether ),您可以编写如下内容
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;下面是一个完整的编译示例
#include <array>
#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, 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)); }
};
int main ()
{
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;
static_assert( std::is_same<Homomorphism<int, long long>,
decltype(h2)::comp>{}, "!" );
static_assert( std::is_same<Monomorphism<int, long long>,
decltype(m2)::comp>{}, "!" );
}https://stackoverflow.com/questions/50865245
复制相似问题