引用模板化的数据成员 class/object
Data member referencing templated class/object
如何将对模板化 class 对象的引用放置在不同的(模板化的,尽管我不确定是否相关)class?
目前我有(简体):
template <typename T, size_t D>
class Field
{
public:
template <size_t meshDim>
Field(const Mesh<meshDim>& mesh, const std::string &fileName):
fileName_(fileName),
meshDim_(meshDim),
mesh_(mesh) // <- this also doesn't compile,
// although I think it would if I had a data member named
// mesh_ that compiled
{
for (unsigned int d=0; d<D; d++) {
field_[d].reserve(mesh.numCells());
}
}
private:
std::vector<T> field_[D];
std::string fileName_;
const size_t meshDim_;
template<size_t meshDim>
const Mesh<meshDim> &mesh_; // <- This doesn't compile
};
这会遇到编译时错误:data member 'mesh_' cannot be a member template
。关于 variable templates 的 link 让我觉得我正在尝试做的事情(或者至少,类似于我正在尝试做的事情)应该可以用 c++14
,但可能不是 c++11
.
如果我删除 const Mesh<meshDim> &mesh_;
行之前的 template<size_t meshDim>
行(也删除模板参数,以避免未定义 meshDim
),然后我被告知我是使invalid use of template-name 'Mesh' without an argument list
,这是有道理的。
如果我在 <>
中离开但没有参数(不是我希望它起作用,而是尝试任何方法),我得到 wrong number of template arguments (0, should be 1)
.
这可能吗?我是否需要制作 some/all 事物的 some/all 部分 static
或者 constexpr
?
原则上,应该只有一个 Mesh
对象,我可以尝试将其设为 constexpr
构造函数,因为它需要的参数可能是 #define
如果需要,由构建系统提供。
在您当前的代码中,您的字段 classes 行为明确取决于 meshDim
。至少那是您的代码所说的。所以你需要在网格尺寸上对其进行参数化:
template< typename T, size_t D, size_t meshDim>
class Field {
// ...
Field(mesh<meshDim> & m);
// ...
mesh<meshDim> & mesh_;
};
如果场的行为不直接取决于网格大小,例如它可以采用任何网格,然后你需要给它一个不依赖于网格大小的 class 的引用:
class IMesh {
virtual void doStuff(void) = 0; // your interface
// ...
};
template<size_t meshDim>
class Mesh : public IMesh { // vtable or similar means required now
// ...
};
template< typename T, size_t D>
class Field {
// ...
Field(IMesh &);
// ...
IMesh & mesh_; // Reference, so no slicing
};
关于变量模板:它们不会解决您的问题:
When used at class scope, variable template declares a static data member template.
(Source)
... 这是有道理的,因为如果你可以为非静态数据成员提供变量模板,就不可能在对象声明时计算对象的大小,因为那样你就不知道实例化是什么制作。
您引用的页面说它们只能是静态数据成员。
删除 rtemplate 声明并插入一个数字
例如 const Mesh<15> &mesh_;确实编译所以看起来问题出在模板上。
另外N3651不建议模板参数可以用作变量的模板参数。 (虽然这似乎是模棱两可的。)
如何将对模板化 class 对象的引用放置在不同的(模板化的,尽管我不确定是否相关)class?
目前我有(简体):
template <typename T, size_t D>
class Field
{
public:
template <size_t meshDim>
Field(const Mesh<meshDim>& mesh, const std::string &fileName):
fileName_(fileName),
meshDim_(meshDim),
mesh_(mesh) // <- this also doesn't compile,
// although I think it would if I had a data member named
// mesh_ that compiled
{
for (unsigned int d=0; d<D; d++) {
field_[d].reserve(mesh.numCells());
}
}
private:
std::vector<T> field_[D];
std::string fileName_;
const size_t meshDim_;
template<size_t meshDim>
const Mesh<meshDim> &mesh_; // <- This doesn't compile
};
这会遇到编译时错误:data member 'mesh_' cannot be a member template
。关于 variable templates 的 link 让我觉得我正在尝试做的事情(或者至少,类似于我正在尝试做的事情)应该可以用 c++14
,但可能不是 c++11
.
如果我删除 const Mesh<meshDim> &mesh_;
行之前的 template<size_t meshDim>
行(也删除模板参数,以避免未定义 meshDim
),然后我被告知我是使invalid use of template-name 'Mesh' without an argument list
,这是有道理的。
如果我在 <>
中离开但没有参数(不是我希望它起作用,而是尝试任何方法),我得到 wrong number of template arguments (0, should be 1)
.
这可能吗?我是否需要制作 some/all 事物的 some/all 部分 static
或者 constexpr
?
原则上,应该只有一个 Mesh
对象,我可以尝试将其设为 constexpr
构造函数,因为它需要的参数可能是 #define
如果需要,由构建系统提供。
在您当前的代码中,您的字段 classes 行为明确取决于 meshDim
。至少那是您的代码所说的。所以你需要在网格尺寸上对其进行参数化:
template< typename T, size_t D, size_t meshDim>
class Field {
// ...
Field(mesh<meshDim> & m);
// ...
mesh<meshDim> & mesh_;
};
如果场的行为不直接取决于网格大小,例如它可以采用任何网格,然后你需要给它一个不依赖于网格大小的 class 的引用:
class IMesh {
virtual void doStuff(void) = 0; // your interface
// ...
};
template<size_t meshDim>
class Mesh : public IMesh { // vtable or similar means required now
// ...
};
template< typename T, size_t D>
class Field {
// ...
Field(IMesh &);
// ...
IMesh & mesh_; // Reference, so no slicing
};
关于变量模板:它们不会解决您的问题:
When used at class scope, variable template declares a static data member template. (Source)
... 这是有道理的,因为如果你可以为非静态数据成员提供变量模板,就不可能在对象声明时计算对象的大小,因为那样你就不知道实例化是什么制作。
您引用的页面说它们只能是静态数据成员。
删除 rtemplate 声明并插入一个数字 例如 const Mesh<15> &mesh_;确实编译所以看起来问题出在模板上。
另外N3651不建议模板参数可以用作变量的模板参数。 (虽然这似乎是模棱两可的。)