繁体   English   中英

char 指针的模板特化?

[英]Template specialization for char pointer?

boost::lexical_cast是一个很棒的工具,但在我的应用程序中,我遇到了string -> bool转换的限制,这让我很困扰。 我需要将所有字符串如"0""false""FALSE"转换为false"1""true""TRUE"转换为true

boost::lexical_cast仅支持从/到"0""1"的转换。 所以我的想法是编写我自己的转换 function 似乎工作正常:

bool str_to_bool(const std::string &str)
{
    if(str == "1" || str == "true" || str == "TRUE")
        return true;
    else if(str == "0" || str == "false" || str == "FALSE")
        return false;
    else
        throw std::runtime_error("Bad cast from std::string to bool!");
}

现在我想编写一个包装器boost::lexical_cast并为它编写我自己的模板特化。 这是我到目前为止所得到的:

template<typename Target, typename Source>
inline Target my_cast(const Source& src)
{
    return boost::lexical_cast<Target>(src);
}

template<>
inline bool my_cast(const std::string& src)
{
    return str_to_bool(src);
}

这对于整数或 std::string 非常有效,但对于字符串文字或字符指针显然失败:

int main(int argc, char* argv[])
{
    std::cout << my_cast<bool>(1) << std::endl;                    //OK
    std::cout << my_cast<bool>(std::string("true")) << std::endl;  //OK
    std::cout << my_cast<bool>("true") << std::endl;               //Fail! 

    return 0;
}

所以我尝试为char *编写另一个专业化,但它无法编译!

//does not compile!
template<>
inline bool my_cast(const char*& src)
{
    return str_to_bool(src);
}

支持 std::string 和char *的正确方法是什么?

编辑1:标题很愚蠢。 解决它。

编辑 2:我从 boost 本身借了一个解决方案。 发布为新答案

如果你这样说:

template<>
inline bool my_cast<bool, std::string>(std::string const & src)
{
  return str_to_bool(src);
}

template<>
inline bool my_cast<bool, const char *>(const char * const & src)
{
  return str_to_bool(src);
}

那么至少您可以进行以下工作:

int main(int argc, char* argv[])
{
  const char * const q = "true";
  std::cout << my_cast<bool>(q) << std::endl;               //Fail!
  return 0;
}

更新:瞧:

typedef char FT[5];

template<>
inline bool my_cast<bool, FT>(const FT & src)
{
  return str_to_bool(src);
}

这是一个有效的解决方案。 我从boost::lexical_cast本身得到了这个想法:

template<class T>
struct array_to_pointer_decay
{
    typedef T type;
};

template<class T, std::size_t N>
struct array_to_pointer_decay<T[N]>
{
    typedef const T * type;
};

template<typename Target, typename Source>
Target my_cast_internal(const Source& s)
{
    return boost::lexical_cast<Target>(s);
}

template<>
inline bool my_cast_internal(const std::string& src)
{
    return str_to_bool(src);
}

template<>
inline bool my_cast_internal(const char* const& src)
{
    return str_to_bool(src);
}

template<typename Target, typename Source>
inline Target my_cast(const Source& s)
{
    typedef typename array_to_pointer_decay<Source>::type src;

    return my_cast_internal<Target, src>(s);
}

主要挑战是处理数组类型。 array_to_pointer_decay将任何数组类型转换为相应的指针类型。 rest 现在很容易。

您需要使用const char* ,而不是const char*& 这里的可变左值引用只会绑定到左值,而字符串文字实际上是数组类型的衰减只会产生右值const char* ,您只能将其绑定到 const 引用。

让我将此添加为新答案……类型擦除版本!

对于 C++98/03

/* Core caster */
bool str_to_bool(const std::string &str)
{
  if(str == "1" || str == "true" || str == "TRUE")
    return true;
  else if(str == "0" || str == "false" || str == "FALSE")
    return false;
  else
    throw std::runtime_error("Bad cast from std::string to bool!");
}


/* Type erasing scaffold */

struct TypeEraseBase
{
  virtual bool cast() const = 0;
  virtual ~TypeEraseBase() { }
};

template <typename T>
struct TypeEraseImpl : public TypeEraseBase
{
  TypeEraseImpl(const T & tt) : t(tt) { }
  virtual bool cast() const { return boost::lexical_cast<T>(t); }
private:
  const T & t;
};

/* Specializations go here */

template <>
struct TypeEraseImpl<std::string> : public TypeEraseBase
{
  TypeEraseImpl(const std::string & tt) : t(tt) { }
  virtual bool cast() const { return str_to_bool(t); }
private:
  const std::string & t;
};

template <size_t N>
struct TypeEraseImpl<char[N]> : public TypeEraseBase
{
  TypeEraseImpl(const char (& tt)[N]) : t(tt) { }
  virtual bool cast() const { return str_to_bool(std::string(t)); }
private:
  const char (& t)[N];
};

template <>
struct TypeEraseImpl<const char *> : public TypeEraseBase
{
  TypeEraseImpl(const char * const & tt) : t(tt) { }
  virtual bool cast() const { return str_to_bool(std::string(t)); }
private:
  const char * const & t;
};


/* User interface class */

struct my_cast
{
  template <typename T> my_cast(const T & tt)
  : pt(new TypeEraseImpl<T>(tt))
  {
  }

  ~my_cast() { if (pt) delete pt; }

  inline bool cast() const { return pt->cast(); }

private:
  const TypeEraseBase * const pt;
};


// Usage example

int main()
{
  const char * const q = "true";
  std::cout << my_cast(1).cast() << std::endl;
  std::cout << my_cast(std::string("true")).cast() << std::endl;
  std::cout << my_cast("true").cast() << std::endl;
  std::cout << my_cast(q).cast() << std::endl;

  return 0;
}

类型特征版本,模板化返回类型

#include <string>
#include <stdexcept>
#include <iostream>
#include <ostream>
#include <boost/lexical_cast.hpp>

template <typename T> struct is_string : std::false_type { };
template <> struct is_string<std::string> : std::true_type { };
template <> struct is_string<const char *> : std::true_type { };
template <std::size_t N> struct is_string<char[N]> : std::true_type { };


/* The actual caster class */

template <typename T, bool B> struct to_bool
{
  static inline bool cast(const T & t)
  {
    return boost::lexical_cast<T>(t);
  }
};

template <typename T> struct to_bool<T, true>
{
  static inline bool cast(const T & t)
  {
    const std::string str(t);
    if(str == "1" || str == "true" || str == "TRUE")
      return true;
    else if(str == "0" || str == "false" || str == "FALSE")
      return false;
    else
      throw std::runtime_error("Bad cast from std::string to bool!");
  }
};


/* Type erasing helper class */

template <typename Target>
struct TypeEraseBase
{
  virtual Target cast() const = 0;
  virtual ~TypeEraseBase() { }
};

template <typename T, typename Target>
struct TypeEraseImpl : public TypeEraseBase<Target>
{
  TypeEraseImpl(const T & tt) : t(tt) { }
  virtual Target cast() const { return boost::lexical_cast<T>(t); }
private:
  const T & t;
};

template <typename T>
struct TypeEraseImpl<T, bool> : public TypeEraseBase<bool>
{
  TypeEraseImpl(const T & tt) : t(tt) { }
  virtual bool cast() const { return to_bool<T, is_string<T>::value>::cast(t); }
private:
  const T & t;
};


/* User interface class */

template <typename Target>
struct my_cast
{
  template <typename T> my_cast(const T & tt)
    : pt(new TypeEraseImpl<T, Target>(tt)) { }

  ~my_cast() { if (pt) delete pt; }

  inline Target cast() const { return pt->cast(); }

private:
  const TypeEraseBase<Target> * const pt;
};

template <typename Target>
std::ostream & operator<<(std::ostream & stream, const my_cast<Target> & c)
{ return stream << c.cast(); }


/* Usage */

int main()
{
  const char * const q = "true";
  std::cout << my_cast<bool>(1) << std::endl;
  std::cout << my_cast<bool>(std::string("true")) << std::endl;
  std::cout << my_cast<bool>("true") << std::endl;
  std::cout << my_cast<bool>(q) << std::endl;

  return 0;
}

暂无
暂无

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

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