英文:
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<typename T>
using refval= std::pair<int, T>;
template<typename T>
using indexedvals = std::vector<refval<T>>;
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<typename T>
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<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>>;
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<class T> using indexedvals = std::vector<refval_t>’
23 | using indexedvals = std::vector<refval<T>>;
| ^~~~~~~~~~~
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<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;
};
and then you can define an alias template:
template<typename T>
using refval = refval_t<T>;
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论