对象;i、 例如: struct TypeInfo { TypeInfo(std::size_t elm_size, const std::string& type_name) : elm_size(elm_size), type_name(type_name) {} std::size_t elm_size; std::string type_name; } TypeInfo double_info(sizeof double, "double"); TypeInfo int_info(sizeof int, "int"); TypeInfo<double> double_info; // <- This does not work,c++,templates,C++,Templates" /> 对象;i、 例如: struct TypeInfo { TypeInfo(std::size_t elm_size, const std::string& type_name) : elm_size(elm_size), type_name(type_name) {} std::size_t elm_size; std::string type_name; } TypeInfo double_info(sizeof double, "double"); TypeInfo int_info(sizeof int, "int"); TypeInfo<double> double_info; // <- This does not work,c++,templates,C++,Templates" />

使用模板化构造函数实例化非模板类 我有一个小C++类,它封装了一些基本类型的类型信息;简化版本如下所示: struct TypeInfo { TypeInfo(std::size_t elm_size, const std::string& type_name) : elm_size(elm_size), type_name(type_name) {} std::size_t elm_size; std::string type_name; } TypeInfo double_info(sizeof double, "double"); TypeInfo int_info(sizeof int, "int"); TypeInfo<double> double_info; // <- This does not work 这是有效的,但是我希望能够基于普通C++模板实例化 TypeInfo < /Cord>对象;i、 例如: struct TypeInfo { TypeInfo(std::size_t elm_size, const std::string& type_name) : elm_size(elm_size), type_name(type_name) {} std::size_t elm_size; std::string type_name; } TypeInfo double_info(sizeof double, "double"); TypeInfo int_info(sizeof int, "int"); TypeInfo<double> double_info; // <- This does not work

使用模板化构造函数实例化非模板类 我有一个小C++类,它封装了一些基本类型的类型信息;简化版本如下所示: struct TypeInfo { TypeInfo(std::size_t elm_size, const std::string& type_name) : elm_size(elm_size), type_name(type_name) {} std::size_t elm_size; std::string type_name; } TypeInfo double_info(sizeof double, "double"); TypeInfo int_info(sizeof int, "int"); TypeInfo<double> double_info; // <- This does not work 这是有效的,但是我希望能够基于普通C++模板实例化 TypeInfo < /Cord>对象;i、 例如: struct TypeInfo { TypeInfo(std::size_t elm_size, const std::string& type_name) : elm_size(elm_size), type_name(type_name) {} std::size_t elm_size; std::string type_name; } TypeInfo double_info(sizeof double, "double"); TypeInfo int_info(sizeof int, "int"); TypeInfo<double> double_info; // <- This does not work,c++,templates,C++,Templates,如果我可以基于模板参数实例化TypeInfo实例,那将非常有用。由于我只需要介绍少数基本类型-float、double、int和char,我非常乐意明确地专门化这少数类型 更新:@nathan_oliver建议将整个类作为模板,然后使用sizeof(T)确定元素大小。该解决方案的问题(在我看来)-是用户仍然需要提供type\u name字符串(以及更多特定于类型的信息)-我想专门化我需要的几个类型-然后完全指定: template <typename T> struct TypeIn

如果我可以基于模板参数实例化
TypeInfo
实例,那将非常有用。由于我只需要介绍少数基本类型-
float、double、int
char
,我非常乐意明确地专门化这少数类型

更新:@nathan_oliver建议将整个类作为模板,然后使用
sizeof(T)
确定元素大小。该解决方案的问题(在我看来)-是用户仍然需要提供
type\u name
字符串(以及更多特定于类型的信息)-我想专门化我需要的几个类型-然后完全指定:

template <typename T>
struct TypeInfo {
    TypeInfo();

    std::size_t elm_size;
    std::string type_name;
}

由于无法调用构造函数,因此无法将类构造函数设为模板并指定模板类型

您可以将类设置为模板,并使用模板类型获取类型的大小,而不是由用户提供。那会给你

template<typename T>
struct TypeInfo {
    TypeInfo(const std::string& type_name) : 
        type_name(type_name)
    {}

    std::size_t elm_size = sizeof(T);
    std::string type_name;    
};

TypeInfo<double> double_info("double");
TypeInfo<int> int_info("int");
模板
结构类型信息{
TypeInfo(常量标准::字符串和类型名称):
类型名称(类型名称)
{}
std::size\u t elm\u size=sizeof(t);
std::字符串类型\u名称;
};
TypeInfo double_info(“double”);
类型信息int_信息(“int”);

不能为构造函数指定模板参数。它们只能推断出来。您可以向构造函数发送类型标记:

template<typename> struct tag_t {};
template<typename T> inline constexpr auto tag = tag_t<T>{}; 

struct TypeInfo {
    template<typename T>
    TypeInfo(tag_t<T>) :
        elm_size{sizeof(T)},
        type_name{/* find a way to get name */} {}

    std::size_t elm_size;
    std::string type_name;  
};

TypeInfo double_info{tag<double>};
TypeInfo int_info{tag<int>};
模板结构标记{};
模板内联constexpr auto tag=tag\u t{};
结构类型信息{
模板
类型信息(标记):
elm_size{sizeof(T)},
键入_name{/*查找获取name*/}{}的方法
标准::大小和尺寸;
std::字符串类型\u名称;
};
TypeInfo双_info{tag};
TypeInfo int_info{tag};

您必须找到一种从模板参数获取类型名称的方法。有些库就是这样存在的

另一种解决方案是使用helper函数

struct TypeInfo {
    TypeInfo(std::size_t elm_size, const std::string& type_name) : 
        elm_size(elm_size), 
        type_name(type_name)
    {}

    std::size_t elm_size;
    std::string type_name;    
};

template <typename T>
TypeInfo make_TypeInfo(const std::string& type_name)
{
   return TypeInfo(sizeof(T), type_name);
}
template <typename T>
TypeInfo get_type_info() {
    return TypeInfo{sizeof(T), "unknown"};
}

template <>
TypeInfo get_type_info<int>() {
    return TypeInfo{sizeof(T), "int"};
}

auto type_info = get_type_info<int>();
struct TypeInfo{
类型信息(std::size\u t elm\u size,const std::string和type\u name):
elm_尺寸(elm_尺寸),
类型名称(类型名称)
{}
标准::大小和尺寸;
std::字符串类型\u名称;
};
模板
类型信息生成类型信息(常量标准::字符串和类型名称)
{
返回TypeInfo(sizeof(T),type_name);
}
和使用

TypeInfo double_info = make_TypeInfo<double>("double");
TypeInfo int_info = make_TypeInfo<int>("int");
TypeInfo-double\u-info=make\u-TypeInfo(“double”);
TypeInfo int_info=make_TypeInfo(“int”);

如果您确实只对一些基本类型感兴趣,并且可以手动对它们进行专门化,那么这似乎是一个很好的方法

template <typename T>
struct TypeInfo; // default values could go here for un-specified types

template <>
struct TypeInfo<int> {
    const std::size_t elm_size = sizeof(T);
    const std::string type_name = "int";
}

// more specializations for double, char and float

你打错了
TypeInfo::TypeInfo()
应该是
TypeInfo::TypeInfo()
此外,您不能将模板放入cpp文件中:谢谢-该输入错误不是输入错误;它缺乏能力。我在cpp文件中有模板专门化-这应该行得通吗?如果其他翻译单位使用,cpp中的专门化将不起作用。所有的模板代码都应该在一个头文件中。是的-我考虑过这一点,再加上对我的编译错误的一个有用的评论,这似乎是最终的解决方案;非常感谢。
template <typename T>
TypeInfo get_type_info() {
    return TypeInfo{sizeof(T), "unknown"};
}

template <>
TypeInfo get_type_info<int>() {
    return TypeInfo{sizeof(T), "int"};
}

auto type_info = get_type_info<int>();