Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用整数值作为模板参数在函数模板中进行参数推导_C++_Templates_Generic Programming - Fatal编程技术网

C++ 使用整数值作为模板参数在函数模板中进行参数推导

C++ 使用整数值作为模板参数在函数模板中进行参数推导,c++,templates,generic-programming,C++,Templates,Generic Programming,我试图实现一个通用函数,该函数从一个id(一个std::pair)生成一个std::string 功能如下: typedef uint32_t element_type; template <element_type type> std::string to_string (const std::pair<element_type, uint32_t>& id) { .... const char* name = elemen_type_trai

我试图实现一个通用函数,该函数从一个id(一个
std::pair
)生成一个
std::string

功能如下:

typedef uint32_t element_type;

template <element_type type>
std::string to_string (const std::pair<element_type, uint32_t>& id) {
    ....
    const char* name = elemen_type_traits<type>::element_type_name;
    ...
}

提前谢谢

如果在类型中对值进行编码,这实际上是可以实现的:

// C++11 'enum class' emulation, you don't want to leak 'foo' everywhere
// also called a "scoped enum"
struct element_type_value{
   enum type{
     foo = 1337
   };
};

template<element_type_value::type V>
struct element_type{};

template<element_type_value::type V>
std::string to_string(std::pair<element_type<V>, uint32_t> const& /*id*/){
  // ...
  const char* name = element_type_traits<V>::element_type_name;
  // ...
}

如果我理解正确,您只需写下:

std::string to_string (const std::pair<element_type, uint32_t>& id) {
 const element_type type = id.first;
 ....
 const char* name = elemen_type_traits<type>::element_type_name;
 ...
}
std::string到_string(const std::pair&id){
常量元素_type type=id.first;
....
常量字符*名称=元素类型特征::元素类型名称;
...
}

是否可以将该对用作模板参数而不是元素?类型实际上是类型吗?它应该是uint32\u t类型的值,不是吗?@Gir我尝试过,但没有成功。我收到一条错误消息:调用“to_string”时没有匹配的函数@klm123 type是类型为_value的值(即uint32_t)。这将不起作用,因为您必须在编译时知道“type”值。但是编写的函数在您想要做的事情方面是正确的?那就没有办法了。只是因为id.first在编译时是未知的。这就是我使用模板函数的原因
// C++11 'enum class' emulation, you don't want to leak 'foo' everywhere
// also called a "scoped enum"
struct element_type_value{
   enum type{
     foo = 1337
   };
};

template<element_type_value::type V>
struct element_type{};

template<element_type_value::type V>
std::string to_string(std::pair<element_type<V>, uint32_t> const& /*id*/){
  // ...
  const char* name = element_type_traits<V>::element_type_name;
  // ...
}
struct element_type{
   enum type{
     foo = 1337
   };
};

template<element_type::type V>
struct element_type_id{
  element_type_id(uint32_t id) : id(id){}
  uint32_t id; // or whatever your original std::pair::second represented
};

template<element_type::type V>
std::string to_string(element_type_id<V> const& /*id*/){
  // ...
  const char* name = element_type_traits<V>::element_type_name;
  // ...
}
std::string to_string (const std::pair<element_type, uint32_t>& id) {
 const element_type type = id.first;
 ....
 const char* name = elemen_type_traits<type>::element_type_name;
 ...
}