将void函数模板专门化为const char [N] [英] Specialize a void function template to a const char[N]

查看:63
本文介绍了将void函数模板专门化为const char [N]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板化函数,我想将 foo 专门化为 const char [N] (硬编码字符串)

I have a templated function which I want to specialize foo to const char[N] (hardcoded strings)

    template<typename T>
const std::string foo() ;

    template<typename T,int N>
const std::string foo<T[N]>() { return "T[N]"; } //this doesn't work for const char[12]

    template<>
const std::string foo<const char*>() { return "Const char"; } //this doesn't work for const char[12]

   template<typename T>
   void someother function(T obj)
   {

   string s = foo<T>(); //I want to overload when T is const chat[N]
   }

   function("Hello World");  //When calling like this the type is const char[12]

我认为我可以做类似的事情 此处 .
但这并不能很好地起作用,因为我没有传递参数,而只是传递了模板化类型.
我可以那样做,但是没有理由将参数传递给该函数.

I thought I can do something like was done Here.
But it doesn't work as well, because I'm not passing a parameter, just a templated type.
I could do it like that, but there's just no reason to pass a parameter to that function.

该示例不起作用,因为我没有传递变量.尝试了几件事,但无法正常工作.

The example doesn't work because I'm not passing a variable. Tried a few things, but can't get it to work.

这是我无法解决的唯一专业.我已经将函数专门用于int,string和其他类型,并且它们工作正常.

This is the only specialization I'm not able to solve. I've specialized the function for int,string and other types and they work OK.

 error LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const __cdecl foo<char const [12]>(void)" 

第一个模板化声明没有任何故意的代码……我试图获得适用于我的案例的正确专业化.

The first templated declaration doesn't have any code in purpose... I'm trying to get the right specialization that will be used for my case.

推荐答案

您必须分两个步骤进行操作.由于您不能部分专门化一个函数,因此必须让该函数调用一个可以部分专门化的类.因此,下面的方法将起作用.

You have to do it in two steps. Since you can't partially specialize a function, you have to have the function call a class, which can be partially specialized. So the below will work.

#include <typeinfo>
#include <iostream>

namespace detail {

  template <typename T> struct foo_helper {
    static std::string helper() {
      return typeid(T).name();
    }
  };

  template <int N> struct foo_helper<const char [N]> {
    static std::string helper() {
      return "Const Char Array";
    }
  };
}

template <typename T> std::string foo(T& obj) {
  return detail::foo_helper<T>::helper();
}

int main() {
  std::string x;
  const char c[] = "hard coded";
  std::cout << foo(x) << std::endl;
  std::cout << foo(c) << std::endl;
}

这正确地调用了常量字符串的专业化.我也将 T obj 更改为 T&obj ,因此g ++会将静态字符串作为数组而不是指针传递.有关局部专业化的更多详细信息,请参见 http://www.gotw.ca/publications/mill17.htm

This calls the specialization properly for the constant string. I also changed T obj into T& obj, so g++ would pass the static strings as arrays, not pointers. For more detail on partial specialization, look at http://www.gotw.ca/publications/mill17.htm

这篇关于将void函数模板专门化为const char [N]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆