如何使用模板和/或使用结构体?

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

How to use templates and/or using`with structs?

问题

以下是您要翻译的部分:

  1. 我有以下定义:
  2. template<typename T>
  3. using refval= std::pair<int, T>;
  4. template<typename T>
  5. using indexedvals = std::vector<refval<T>>;
  6. 为了使我的代码更易读,我想在“refval”的定义中使用“struct”而不是“pair”,这样在我的代码中可以使用成员名称“index”代替“first”,或者使用“value”代替“second”。
  7. 所以我尝试了:
  8. template<typename T>
  9. typedef struct_t refval {
  10. refval(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
  11. int m_iRefCell;
  12. T m_tValue;
  13. } refval;
  14. 但是这里编译器报错“error: template declaration of typedef’”。
  15. 然后我尝试了:
  16. template<typename T>
  17. using refval = struct refval_t {
  18. refval_t():m_iRefCell(-1){};
  19. refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
  20. int m_iRefCell;
  21. T m_tValue;
  22. };
  23. template<typename T>
  24. using indexedvals = std::vector<refval<T>>;
  25. 这里我得到了以下投诉:
  26. Sampling.h:12:32: error: types may not be defined in alias template declarations
  27. 12 | using refval = struct refval_t {
  28. | ^
  29. Sampling.h:23:7: error: conflicting declaration of template template<class T> using indexedvals = std::vector<refval_t>
  30. 23 | using indexedvals = std::vector<refval<T>>;
  31. | ^~~~~~~~~~~
  32. 所以我想要做的是以一种方式定义我的模板结构,以便我可以像本帖开头的代码片段中那样在“using”语句中使用它。
  33. 我如何实现这一目标?
  34. (可能一个类似的问题是:如何实现“std::pair”以使其正常工作?)
英文:

I have the following definitions:

  1. template&lt;typename T&gt;
  2. using refval= std::pair&lt;int, T&gt;;
  3. template&lt;typename T&gt;
  4. using indexedvals = std::vector&lt;refval&lt;T&gt;&gt;;

In order to make my code more readable i wanted to use a struct instead of a pair in the definition of refval - so that in my code i could use a member name like index instead of first, or value instead of second.

So i tried

  1. template&lt;typename T&gt;
  2. typedef struct_t refval {
  3. refval(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
  4. int m_iRefCell;
  5. T m_tValue;
  6. } refval;

But here the compiler complained with "error: template declaration of ‘typedef’".

Then i tried

  1. template&lt;typename T&gt;
  2. using refval = struct refval_t {
  3. refval_t():m_iRefCell(-1){};
  4. refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
  5. int m_iRefCell;
  6. T m_tValue;
  7. };
  8. template&lt;typename T&gt;
  9. using indexedvals = std::vector&lt;refval&lt;T&gt;&gt;;

Here i got the following complaints

  1. Sampling.h:12:32: error: types may not be defined in alias template declarations
  2. 12 | using refval = struct refval_t {
  3. | ^
  4. Sampling.h:23:7: error: conflicting declaration of template template&lt;class T&gt; using indexedvals = std::vector&lt;refval_t&gt;’
  5. 23 | using indexedvals = std::vector&lt;refval&lt;T&gt;&gt;;
  6. | ^~~~~~~~~~~

So what i want to do is to define my templated struct in such a way that i can use it in a using statement as in the code snippets at the beginning of this post.

How can i achieve this?

(probably an analogous question would be: how is std::pair implemented so that this works?)

答案1

得分: 4

坦率地说,你正在猜测它是如何工作的,这永远不会有好结果。

你可以定义一个模板:

  1. template<typename T>
  2. struct refval_t {
  3. refval_t():m_iRefCell(-1){};
  4. refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){}
  5. int m_iRefCell;
  6. T m_tValue;
  7. };

然后你可以定义一个别名模板:

  1. template<typename T>
  2. using refval = refval_t<T>;

然而,我认为实际上你并不需要这个别名,因为你可以直接使用模板。

请注意,在默认构造函数中也应初始化 m_tValue 成员。按照你当前的写法,很容易在它初始化之前就读取它,并引发未定义的行为。

英文:

Frankly, you are guessing how it works, and this is never going to end well.

You can define a template:

  1. template&lt;typename T&gt;
  2. struct refval_t {
  3. refval_t():m_iRefCell(-1){};
  4. refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){}
  5. int m_iRefCell;
  6. T m_tValue;
  7. };

and then you can define an alias template:

  1. template&lt;typename T&gt;
  2. using refval = refval_t&lt;T&gt;;

However, I don't think you actually need the alias when you can just use the template directly.

Note that you should initialize the m_tValue member also in the default constructor. As you have it currently, it is too easy to read from it before it is initialized and invoke undefined behavior.

huangapple
  • 本文由 发表于 2023年4月17日 21:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035867.html
匿名

发表评论

匿名网友

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

确定