可以写一个概念来测试是否存在一个”模板成员”。

huangapple go评论53阅读模式
英文:

Can I write a concept to test for the existence of a "templated member"

问题

  1. 可以编写一个概念来检查成员是否为模板吗(而不只是尝试任意特化)?

例如,检查类型是否包含一个接受模板参数的函数 zug<T>(T)

struct SNoZug {};

struct SZug
{
  template <typename T> // (A)
  void zug(T) {};
};

template <typename T>
concept has_zug = ???; // << 可以吗?

static_assert(!has_zug<SNoZug>);
static_assert(has_zug<SZug>);
  1. 如果 (A) 使用概念参数(例如:template <some_other_concept T>),这能做到吗?
  2. 如果 zug(A)使用可变参数,这能做到吗?
  3. 如果 zug 使用值参数,这能做到吗?
  4. 如果 zug 使用自动参数,这能做到吗?
  5. 如果 zug 使用模板模板参数,这能做到吗?
  6. 如果 zug 是成员类而不是成员函数,这能做到吗?

注意:相关未回答的问题:https://stackoverflow.com/questions/72522317/check-the-existence-of-a-member-function-template-in-a-concept-definition。

注意:这里有一些针对可能相关问题的提出的代码,但我不确定它是否是有效的 C++:https://stackoverflow.com/questions/68238264/c20-template-template-concept-syntax/68381246#comment134010477_68381246。

英文:
  1. Is it possible to write a concept to check for the existence of a member that is a template (without just trying an arbitrary specialization)?

For example, check if the type contains a function zug&lt;T&gt;(T) taking a template parameter.

struct SNoZug {};

struct SZug
  {
    template &lt;typename T&gt; // (A)
    void zug(T) {};
  };

template &lt;typename T&gt;
concept has_zug = ???; // &lt;&lt; CAN THIS BE DONE?

static_assert(!has_zug&lt;SNoZug&gt;);
static_assert(has_zug&lt;SZug&gt;);
  1. Can this be done if (A) uses a concept parameter (eg: template &lt;some_other_concept T&gt;)?
  2. Can this be done if zug (A) uses a variadic parameter?
  3. Can this be done if zug uses a value parameter?
  4. Can this be done if zug uses an auto parameter?
  5. Can this be done if zug uses a template template parameter?
  6. Can this be done if zug was a member class instead of a member function?

NOTE: Related unanswered question: https://stackoverflow.com/questions/72522317/check-the-existence-of-a-member-function-template-in-a-concept-definition.

NOTE: There's some proposed code for a potentially related problem here, but I'm not sure if it's valid C++: https://stackoverflow.com/questions/68238264/c20-template-template-concept-syntax/68381246#comment134010477_68381246.

答案1

得分: 1

你可以检查.zug<int>(0)是否有效,或者检查任何固定的实例化(包括那些是其他模板参数的函数)。

这将与测试.zug()的方式相同。

你不能在尝试实例化之前检查通用模板。

在C++添加反射功能后可能会变得可能。

英文:

You can check for .zug&lt;int&gt;(0) being valid, or any fixed instantiation (including those that are functions of the other template arguments).

This will work the same as testing for .zug() would.

You cannot check for a generic template without trying to instantiate.

It may be possible after reflection is added to C++ to do so.

huangapple
  • 本文由 发表于 2023年4月11日 09:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定