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

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

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

问题

以下是您要翻译的部分:

我有以下定义:

template<typename T>
using refval= std::pair<int, T>;

template<typename T>
using indexedvals = std::vector<refval<T>>; 

为了使我的代码更易读,我想在“refval”的定义中使用“struct”而不是“pair”,这样在我的代码中可以使用成员名称“index”代替“first”,或者使用“value”代替“second”。

所以我尝试了:

template<typename T>
typedef struct_t refval {
    refval(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
    int m_iRefCell;
    T   m_tValue;
} refval;

但是这里编译器报错“error: template declaration of ‘typedef’”。

然后我尝试了:

template<typename T>
using refval = struct refval_t {
    refval_t():m_iRefCell(-1){};
    refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
    int m_iRefCell;
    T   m_tValue;
};

template<typename T>
using indexedvals = std::vector<refval<T>>;


这里我得到了以下投诉:

Sampling.h:12:32: error: types may not be defined in alias template declarations
   12 | using refval = struct refval_t {
      |                                ^
Sampling.h:23:7: error: conflicting declaration of template ‘template<class T> using indexedvals = std::vector<refval_t>’
   23 | using indexedvals = std::vector<refval<T>>;
      |       ^~~~~~~~~~~

所以我想要做的是以一种方式定义我的模板结构,以便我可以像本帖开头的代码片段中那样在“using”语句中使用它。

我如何实现这一目标?

(可能一个类似的问题是:如何实现“std::pair”以使其正常工作?)
英文:

I have the following definitions:

template&lt;typename T&gt;
using refval= std::pair&lt;int, T&gt;;

template&lt;typename T&gt;
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

template&lt;typename T&gt;
typedef struct_t refval {
    refval(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
    int m_iRefCell;
    T   m_tValue;
} refval;

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

Then i tried

template&lt;typename T&gt;
using refval = struct refval_t {
    refval_t():m_iRefCell(-1){};
    refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){};
    int m_iRefCell;
    T   m_tValue;
};

template&lt;typename T&gt;
using indexedvals = std::vector&lt;refval&lt;T&gt;&gt;;

Here i got the following complaints

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

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

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

你可以定义一个模板:

template<typename T>
struct refval_t {
    refval_t():m_iRefCell(-1){};
    refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){}
    int m_iRefCell;
    T   m_tValue;
};

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

template<typename T>
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:

template&lt;typename T&gt;
struct refval_t {
    refval_t():m_iRefCell(-1){};
    refval_t(int iRefCell, T tValue):m_iRefCell(iRefCell),m_tValue(tValue){}
    int m_iRefCell;
    T   m_tValue;
};

and then you can define an alias template:

template&lt;typename T&gt;
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:

确定