简体   繁体   English

Const和非const模板专业化

[英]Const and non const template specialization

I have a class template which I use to get the size of a variable: 我有一个类模板,我用它来获取变量的大小:

template <class T>
class Size
{
   unsigned int operator() (T) {return sizeof(T);}
};

This works fine but for strings I want to use strlen instead of sizeof: 这工作正常,但对于字符串我想使用strlen而不是sizeof:

template <>
class Size<char *>
{
   unsigned int operator() (char *str) {return strlen(str);}
};

The problem is when I create an instance of size with const char * it goes to the unspecialized version. 问题是当我使用const char *创建一个大小的实例时,它会进入非专业化版本。 I was wondering if there is a way to capture both the const and non-const versions of char * in on template specialization? 我想知道是否有办法在模板专业化中捕获char *的const和非const版本? Thanks. 谢谢。

Use this technique: 使用这种技术:

#include <type_traits>

template< typename T, typename = void >
class Size
{
  unsigned int operator() (T) {return sizeof(T);}
};

template< typename T >
class Size< T, typename std::enable_if<
                 std::is_same< T, char* >::value ||
                 std::is_same< T, const char* >::value
               >::type >
{
  unsigned int operator() ( T str ) { /* your code here */ }
};

EDIT: Example of how to define the methods outside of the class definition. 编辑:如何在类定义之外定义方法的示例。

EDIT2: Added helper to avoid repeating the possibly long and complex condition. EDIT2:添加帮助以避免重复可能长而复杂的情况。

EDIT3: Simplified helper. EDIT3:简化帮手。

#include <type_traits>
#include <iostream>

template< typename T >
struct my_condition
  : std::enable_if< std::is_same< T, char* >::value ||
                    std::is_same< T, const char* >::value >
{};

template< typename T, typename = void >
struct Size
{
  unsigned int operator() (T);
};

template< typename T >
struct Size< T, typename my_condition< T >::type >
{
  unsigned int operator() (T);
};

template< typename T, typename Dummy >
unsigned int Size< T, Dummy >::operator() (T)
{
  return 1;
}

template< typename T >
unsigned int Size< T, typename my_condition< T >::type >::operator() (T)
{
  return 2;
}

int main()
{
  std::cout << Size< int >()(0) << std::endl;
  std::cout << Size< char* >()(0) << std::endl;
  std::cout << Size< const char* >()(0) << std::endl;
}

which prints 打印

1
2
2

One way is to use a helper function in order to determine if the template type is a char * or a char const * . 一种方法是使用辅助函数来确定模板类型是char *还是char const * You can do this with a simple struct. 您可以使用简单的结构来完成此操作。 Then you can use SFINAE to select the proper specialization of your Size struct. 然后,您可以使用SFINAE选择Size结构的正确特化。

#include <cstring>
#include <iostream>

#include <boost/utility/enable_if.hpp>

template<typename T>
struct is_cstring {
  enum { value = false };
};

template<>
struct is_cstring<char *> {
  enum { value = true };
};

template<>
struct is_cstring<char const *> {
  enum { value = true };
};

template<typename T, typename = void>
struct Size;

template<typename T>
struct Size<T, typename boost::disable_if<is_cstring<T> >::type> {
  unsigned int operator ()(T const &) const {
    return sizeof(T);
  }
};

template<typename T>
struct Size<T, typename boost::enable_if<is_cstring<T> >::type> {
  unsigned int operator ()(T const &str) const {
    return strlen(str);
  }
};

int main() {
  std::string blah = "afafasa";
  char *x = "asdfsadsad";

  std::cout << Size<int>()(4) << std::endl;
  std::cout << Size<char const *>()("blahblah") << std::endl;
  std::cout << Size<char *>()(x) << std::endl;
}

The printed result is: 打印结果是:

4
8
10

And you should also be able to write, of course: 当然,你也应该能够写作:

template <>
class Size<const char *>
{
   unsigned int operator() (const char *str) {return strlen(str);}
};

template <>
class Size<char *> : public Size<const char *>
{ };

...and, should you need to: ......而且,如果你需要:

template <size_t size>
class Size<char[size]> : public Size<const char *>
{ };

template <size_t size>
class Size<const char[size]> : public Size<const char *>
{ };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM