C++中一个给定类型的非类型模板模板的正确语法

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

Right syntax for a C++ non-type template template with a given type

问题

I have a class templated over a non-type attribute pointer over a template type, here a std::optional.

  1. #include <optional>
  2. class A
  3. {
  4. public:
  5. std::optional<int> i;
  6. };
  7. template <typename C, typename T, std::optional<T> C::*optionalPtr>
  8. class OptionalHolder3
  9. {
  10. void doStuff(C &c);
  11. };
  12. using OH3 = OptionalHolder3<A, int, &A::i>;

It works well but I was curious if I could make it easier to use, by reducing the number of required template arguments, but I can't find the right syntax:

  1. // Doesn't compile
  2. template <typename C, template<class T> std::optional<T> C::*optionalPtr>
  3. class OptionalHolder2
  4. {};
  5. // Doesn't compile
  6. template <template<class C, class T> std::optional<T> C::*optionalPtr>
  7. class OptionalHolder1
  8. {};
  9. using OH2 = OptionalHolder2<A, &A::i>;
  10. using OH2 = OptionalHolder1<&A::i>;

Is this possible and how do I do it?

英文:

I have a class templated over a non-type attribute pointer over a template type, here a std::optional.

  1. #include &lt;optional&gt;
  2. class A
  3. {
  4. public:
  5. std::optional&lt;int&gt; i;
  6. };
  7. template &lt;typename C, typename T, std::optional&lt;T&gt; C::*optionalPtr&gt;
  8. class OptionalHolder3
  9. {
  10. void doStuff(C &amp;c);
  11. };
  12. using OH3 = OptionalHolder3&lt;A, int, &amp;A::i&gt;;

It works well but I was curious if I could make it easier to use, by reducing the number of required template arguments, but I can't find the right syntax:

  1. // Doesn&#39;t compile
  2. template &lt;typename C, template&lt;class T&gt; std::optional&lt;T&gt; C::*optionalPtr&gt;
  3. class OptionalHolder2
  4. {};
  5. // Doesn&#39;t compile
  6. template &lt;template&lt;class C, class T&gt; std::optional&lt;T&gt; C::*optionalPtr&gt;
  7. class OptionalHolder1
  8. {};
  9. using OH2 = OptionalHolder2&lt;A, &amp;A::i&gt;;
  10. using OH2 = OptionalHolder1&lt;&amp;A::i&gt;;

Is this possible and how do I do it?

答案1

得分: 2

你可以使用 auto (C++17) 来实现这个:

  1. template <auto>
  2. class OptionalHolder; // 无定义
  3. template<typename C, typename T, std::optional<T> C::*optionalPtr>
  4. class OptionalHolder<optionalPtr>
  5. {
  6. void doStuff(C &c);
  7. };

C++20 中的概念可能可以用来替代该特化,但需要一些特性来提取 TC

英文:

You might use auto (C++17) for that:

  1. template &lt;auto&gt;
  2. class OptionalHolder; // No definition
  3. template&lt;typename C, typename T, std::optional&lt;T&gt; C::*optionalPtr&gt;
  4. class OptionalHolder&lt;optionalPtr&gt;
  5. {
  6. void doStuff(C &amp;c);
  7. };

concepts from C++20 might be used instead of that specialization, but it would require some traits to extract T and C.

huangapple
  • 本文由 发表于 2023年6月29日 16:44:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579441.html
匿名

发表评论

匿名网友

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

确定