带有模板 class 作为参数的可变 class 构造函数
variadic class constructor with a templated class as argument
我似乎很难描述我的问题,所以我没有成功搜索以前的答案。
我有下一个用 c++17 编写的代码 -
它有 Data class 可以保存模板化参数(和 size_t 作为 id)。
接下来我有一个数据库 class,它包含一个数据实例元组。
与 main 相比,我有一个具有不同数据类型的数据库实例的示例,但是用户恶意地将一个非数据 class 变量插入到数据库中。我需要阻止它。
// a data to be in the database
template <size_t ID, class T>
class Data
{
public:
Data(T var) : m_var(var) {}
size_t id = ID;
T m_var;
};
//Here is the database. How should I only accept Data class with different template type...
template <class... DATA_TYPES>
class DataBase
{
public:
DataBase(DATA_TYPES... args) : m_data(args...) {}
std::tuple<DATA_TYPES...> m_data;
};
int main() {
DataBase d(Data<0, int>(10),
Data<1, char>(40),
"an invalid member"); //<------- should not be able to compile
}
该代码试图阐明我的问题。我想让 class 数据库接受具有不同模板参数的数据实例(如主函数中所示),但不接受任何其他类型的数据。
如果我将我的模板行概括为 ,也许我可以在 ctor 中使用一些静态断言或“constexpr if”,但应该有一个更改模板行以仅接受具有可变不同类型(和 size_t 作为 ID)模板的数据 class 类型的方法..
将不胜感激!
您需要一种方法来确定类型是否是 Data
的实例化。一种方法是对模板变量使用部分模板特化。
一旦我们有了它,我们就会在 Database
中输入 static_assert
。
#include <tuple>
// a data to be in the database
template <std::size_t ID, class T>
class Data
{
public:
Data(T var) : m_var(var) {}
std::size_t id = ID;
T m_var;
};
template <typename T>
constexpr bool isData = false;
template <std::size_t ID, typename T>
constexpr bool isData<Data<ID, T>> = true;
//Here is the database. How should I only accept Data class with different template type...
template <class... DATA_TYPES>
class DataBase
{
static_assert((isData<DATA_TYPES> && ...));
public:
DataBase(DATA_TYPES... args) : m_data(args...) {}
std::tuple<DATA_TYPES...> m_data;
};
int main() {
DataBase d(Data<0, int>(10),
Data<1, char>(40),
"an invalid member"); //<------- should not be able to compile
}
我似乎很难描述我的问题,所以我没有成功搜索以前的答案。
我有下一个用 c++17 编写的代码 - 它有 Data class 可以保存模板化参数(和 size_t 作为 id)。 接下来我有一个数据库 class,它包含一个数据实例元组。 与 main 相比,我有一个具有不同数据类型的数据库实例的示例,但是用户恶意地将一个非数据 class 变量插入到数据库中。我需要阻止它。
// a data to be in the database
template <size_t ID, class T>
class Data
{
public:
Data(T var) : m_var(var) {}
size_t id = ID;
T m_var;
};
//Here is the database. How should I only accept Data class with different template type...
template <class... DATA_TYPES>
class DataBase
{
public:
DataBase(DATA_TYPES... args) : m_data(args...) {}
std::tuple<DATA_TYPES...> m_data;
};
int main() {
DataBase d(Data<0, int>(10),
Data<1, char>(40),
"an invalid member"); //<------- should not be able to compile
}
该代码试图阐明我的问题。我想让 class 数据库接受具有不同模板参数的数据实例(如主函数中所示),但不接受任何其他类型的数据。
如果我将我的模板行概括为
将不胜感激!
您需要一种方法来确定类型是否是 Data
的实例化。一种方法是对模板变量使用部分模板特化。
一旦我们有了它,我们就会在 Database
中输入 static_assert
。
#include <tuple>
// a data to be in the database
template <std::size_t ID, class T>
class Data
{
public:
Data(T var) : m_var(var) {}
std::size_t id = ID;
T m_var;
};
template <typename T>
constexpr bool isData = false;
template <std::size_t ID, typename T>
constexpr bool isData<Data<ID, T>> = true;
//Here is the database. How should I only accept Data class with different template type...
template <class... DATA_TYPES>
class DataBase
{
static_assert((isData<DATA_TYPES> && ...));
public:
DataBase(DATA_TYPES... args) : m_data(args...) {}
std::tuple<DATA_TYPES...> m_data;
};
int main() {
DataBase d(Data<0, int>(10),
Data<1, char>(40),
"an invalid member"); //<------- should not be able to compile
}