C++ 带模板参数的构造函数
我有一个C++ 带模板参数的构造函数,c++,c++11,templates,constructor,C++,C++11,Templates,Constructor,我有一个Dynamic类,可以存储不同的类型:int,double,std::vector,std::vector,等等。我有大约50种这样的类型 我希望我的动态类型有一个构造函数,其中我们提供两个信息: 要存储的类型 用于在动态类内构造类型的参数 我期待着像这样的事情 const Dynamic x<std::vector<double>>{10}; constdynamicx{10}; 在位构造长度为10的std::vector的动态对象 PS:我被允许使用C
Dynamic
类,可以存储不同的类型:int
,double
,std::vector
,std::vector
,等等。我有大约50种这样的类型
我希望我的动态类型有一个构造函数,其中我们提供两个信息:
- 要存储的类型
- 用于在动态类内构造类型的参数
const Dynamic x<std::vector<double>>{10};
constdynamicx{10};
在位构造长度为10的std::vector
的动态对象
PS:我被允许使用C++11,我不被允许使用RTTI,必须推导构造函数模板参数。它们不能明确提供。您可以通过提供一个类型标记来解决这个问题,该标记对所需的模板参数进行编码,并将其作为额外的构造函数参数传递。例如:
#include <utility> // For std::forward
struct foo
{
// Helper tag type
template<class T>
struct type_tag {};
// The template argument T is deduced from type_tag<T>
template<class T, class ... Args>
foo(type_tag<T>, Args&&... p_args)
{
T value{ std::forward<Args>(p_args)... };
}
};
int main()
{
// Provide a type tag so the template argument can be deduced
foo bar{ foo::type_tag<int>{}, 5 };
}
#包含//用于标准::转发
结构foo
{
//助手标记类型
模板
结构类型_标记{};
//模板参数T是从类型_标记推导出来的
模板
foo(类型标记、参数和…参数)
{
T值{std::forward(p_args)…};
}
};
int main()
{
//提供类型标记,以便可以推断模板参数
foo-bar{foo::type_-tag{},5};
}
必须推导构造函数模板参数。它们不能明确提供。您可以通过提供一个类型标记来解决这个问题,该标记对所需的模板参数进行编码,并将其作为额外的构造函数参数传递。例如:
#include <utility> // For std::forward
struct foo
{
// Helper tag type
template<class T>
struct type_tag {};
// The template argument T is deduced from type_tag<T>
template<class T, class ... Args>
foo(type_tag<T>, Args&&... p_args)
{
T value{ std::forward<Args>(p_args)... };
}
};
int main()
{
// Provide a type tag so the template argument can be deduced
foo bar{ foo::type_tag<int>{}, 5 };
}
#包含//用于标准::转发
结构foo
{
//助手标记类型
模板
结构类型_标记{};
//模板参数T是从类型_标记推导出来的
模板
foo(类型标记、参数和…参数)
{
T值{std::forward(p_args)…};
}
};
int main()
{
//提供类型标记,以便可以推断模板参数
foo-bar{foo::type_-tag{},5};
}
只要您不介意将类型信息放在动态
旁边,而不是变量名称旁边,就可以使用变量参数执行此操作:
#include <iostream>
#include <vector>
template <typename T>
class Dynamic
{
public:
template <typename... Args>
Dynamic(Args... args) : data_(args...)
{
}
T data_;
};
int main()
{
const Dynamic<std::vector<double>> x{10};
std::cout << x.data_.size() << std::endl;
}
#包括
#包括
模板
类动态
{
公众:
模板
动态(Args…Args):数据(Args…)
{
}
T数据;
};
int main()
{
常数动态x{10};
std::cout只要您不介意将类型信息放在Dynamic
旁边,而不是变量名旁边,就可以使用变量参数执行此操作:
#include <iostream>
#include <vector>
template <typename T>
class Dynamic
{
public:
template <typename... Args>
Dynamic(Args... args) : data_(args...)
{
}
T data_;
};
int main()
{
const Dynamic<std::vector<double>> x{10};
std::cout << x.data_.size() << std::endl;
}
#包括
#包括
模板
类动态
{
公众:
模板
动态(Args…Args):数据(Args…)
{
}
T数据;
};
int main()
{
常数动态x{10};
std::难道你不能用构造函数显式地使用模板参数吗?为什么你不能使用RTTI还必须这样做?我不清楚你到底想实现什么,但在模板构造函数上可能是相关的。本质上,你不能为构造函数提供模板参数,它们都必须推导出来。@Passer:我正在设计一个库,我的一些客户在编译时没有RTTI。例如,我需要这样一个动态
对象来加载JSON文件。@François:谢谢。我想我需要忘记这个构造函数。太糟糕了。看这里:你不能用构造函数来显式地使用模板参数。为什么你不能使用RTTI还必须做一些事情呢g像这样?我不清楚你到底想实现什么,但关于模板构造函数可能是相关的。本质上,你不能为构造函数提供模板参数,它们必须全部推导出来。@Passer:我正在设计一个库,我的一些客户在编译时没有RTTI。我需要这样一个动态对象来实现例如ad JSON文件。@François:谢谢。我想我需要忘记这个构造函数。太糟糕了。看这里:Merci bien François Merci bien François这违背了拥有动态type@Passer顺便问一下,你能详细说明一下,把类型放在变量名的左边而不是右边有什么不同吗de仍然是所有静态类型。OP似乎需要类似于std::variant
的东西,这违背了拥有动态type@Passer通过,您能详细说明将类型放在变量名的左边而不是右边有什么不同吗?OP的原始代码仍然都是静态类型。OP似乎需要类似于st的东西d::变体