使用Concepts Lite为具有成员函数模板的类型指定概念 [英] Specifying a concept for a type that has a member function template using Concepts Lite

查看:171
本文介绍了使用Concepts Lite为具有成员函数模板的类型指定概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图指定一个概念来限制一个更高类型的类,它有一个使用Concepts Lite的成员函数模板。不过,我无法在技术规格教程一个处理概念中的模板语句的子句。

I'm trying to specify a concept to constrain a higher kinded type that has a member function template using Concepts Lite. However I am not able to find inside the technical specification or the tutorial a clause dealing with templated statements inside a concept.

这是怎么做的?

示例:假设我有更高的类型 HKT 与会员功能模板 F

Example: suppose I have the higher kinded type HKT with a member function template F:

template<class T>
struct HKT {
  template<class U> // this looks like e.g. rebind in std::allocators
  auto F(U) -> HKT<U>;
};

现在我想指定一个约束这些更高类型类型的概念:

and that now I want to specify a concept for constraining these higher kinded types:

template <template <class> class HKT, class T>
concept HKTWithTemplateMemberFunctionF {
  return requires(HKT<T> h) { // HKT<T> is a type, h is an object
    // HKT<T> needs to have a member function template that 
    // returns HTK<U> where the type U is to be deduced and
    // it can be any type (it is unconstrained)
    template<class U>  // is there a syntax for this?
    h.F(std::declval<U>()) -> HKT<U>; 
  }
}

注意,我可以这样做:

template <template <class> class HKT, class T, class U>
concept HKTWithTemplateMemberFunctionF {
  return requires(HKT<T> h) {
      h.F(std::declval<U>()) -> HKT<U>;
  }
}

但这意味着我需要知道 U

but this means that I need to know U at constraint site.

我真的不在乎是否替换给定的 U 失败,虽然我可以看到为什么这可能是一个问题:例如应用约束以确保您的函数不会失败,然后失败,因为约束已满足,但在实例化时成员函数模板中的替换失败(如果成员函数模板受约束,它会有帮助吗?)。

I don't really care if substitution for a given U fails or not although I can see why this could be a problem: e.g. apply a constraint to be sure your function doesn't fail and then fails cause the constraint was satisfied but at instantiation time substitution failed in the member function template (would it help if the member function template was constrained?).

推荐答案

现在你必须提供一个特定的 U

Long story short, right now you (I?) have to provide a specific U:

template <template <class> class HKT, class T, class U = T>
concept HKTWithTemplateMemberFunctionF {
  return requires(HKT<T> h) { // HKT<T> is a type, h is an object
    h.F(std::declval<U>()) -> HKT<U>; 
  }
}

因为编译器不能证明所有类型 U 可能存在的成员功能模板将工作,即以下是绝望的:

since the compiler cannot prove for all types U that might ever exist that the member-function template will work, that is, the following is hopeless:

template <template <class> class HKT, class T>
concept HKTWithTemplateMemberFunctionF {
  return requires(HKT<T> h) {
    template<class U>  // for all those Us that haven't been written yet...
    h.F(std::declval<U>()) -> HKT<U>; 
  }
}

在假设的5分钟设计概念-lite实现,其中我们能够约束 U 只是一点点

In an hypothetical designed-in-5-min concepts-lite implementation where we are able to constrain U just a little-bit:

template <template <class> class HKT, class T, 
          InputIterator U = InputIterator()  /* imaginary syntax */ >
concept HKTWithTemplateMemberFunctionF {
  return requires(HKT<T> h) {
      h.F(std::declval<U>()) -> HKT<U>; // Is InputIterator enough to instantiate F?
  }
}

编译器只需检查一个模型 InputIterator 足以实例化 hF ,这是可能的,即使 hF 不受约束!另外提供 U 只是检查它模型 InputIterator ,甚至不需要检查 hF U ,因为 InputIterator 这可以用来优化编译时的性能和...

the compiler would only need to check if a model of InputIterator is enough to instantiate h.F, which is possible even if h.F is not constrained! Additionally providing U just checks that it models InputIterator, no need to even try to check h.F for U since InputIterator is enough. This could be used to optimize compile-time performance and...

...可能会以令人惊讶的方式与SFINAE交互,因为AFAIK你可以有一个概念重载函数(例如 InputIterator )接受除了一个之外的所有输入迭代器(SFINAE!为什么会有人这样做?概念检查,但在实例化时间... ...悲伤。

...would probably interact in surprising ways with SFINAE, since AFAIK you can have a concept-overloaded function (e.g. for InputIterator) that accepts all input iterators except that one (SFINAE! why would anyone do that?!), and thus could pass concept-checking but blow at instantiation time... sad.

这篇关于使用Concepts Lite为具有成员函数模板的类型指定概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆