【发布时间】:2014-01-06 02:28:21
【问题描述】:
我正在使用this 神奇的标头来获得轻松序列化 STL 容器的能力。
然而,我现在已经开始为我的类型使用更花哨的 HTML 序列化程序,我想做的一部分是将operator << 功能推广到我的新类型ohtmlstringstream,它由@987654325 支持@。
这是我(正在运行的)尝试这样做(ohtmlstringstream::write 是一个公共模板方法,它将其 arg 传递给私有成员 stringstream 的 operator<<):
namespace std {
template<typename T>
inline typename enable_if< ::pretty_print::is_container<T>::value, ohtmlstringstream&>::type
operator<<(ohtmlstringstream& os, const T& container) {
auto it = std::begin(container);
const auto the_end = end(container);
os.write("<div class='container'>");
while(it != the_end) {
os << *it;
it++;
}
os.write("</div>");
return os;
}
}
我遇到的第一个问题是,任何时候在ohtmlstringstream 上使用std::string,它都会被视为一个容器,这是我不想要的;我想将字符串视为字符串,而不是容器。当然,就pretty_print 而言,std::string 肯定是一个字符容器。
这是prettyprint.hpp的摘录:
namespace pretty_print
{
// SFINAE type trait to detect whether T::const_iterator exists.
template<typename T>
struct has_const_iterator
{
private:
typedef char yes;
typedef struct { char array[2]; } no;
template <typename C> static yes test(typename C::const_iterator*);
template <typename C> static no test(...);
public:
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
typedef T type;
};
// SFINAE type trait to detect whether "T::const_iterator T::begin/end() const" exist.
template <typename T>
struct has_begin_end_OLD
{
struct Dummy { typedef void const_iterator; };
typedef typename std::conditional<has_const_iterator<T>::value, T, Dummy>::type TType;
typedef typename TType::const_iterator iter;
struct Fallback { iter begin() const; iter end() const; };
struct Derived : TType, Fallback { };
template<typename C, C> struct ChT;
template<typename C> static char (&f(ChT<iter (Fallback::*)() const, &C::begin>*))[1];
template<typename C> static char (&f(...))[2];
template<typename C> static char (&g(ChT<iter (Fallback::*)() const, &C::end>*))[1];
template<typename C> static char (&g(...))[2];
static bool const beg_value = sizeof(f<Derived>(0)) == 2;
static bool const end_value = sizeof(g<Derived>(0)) == 2;
};
template <typename T>
struct has_begin_end
{
template<typename C> static char (&f(typename std::enable_if<
std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::begin)),
typename C::const_iterator(C::*)() const>::value, void>::type*))[1];
template<typename C> static char (&f(...))[2];
template<typename C> static char (&g(typename std::enable_if<
std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::end)),
typename C::const_iterator(C::*)() const>::value, void>::type*))[1];
template<typename C> static char (&g(...))[2];
static bool const beg_value = sizeof(f<T>(0)) == 1;
static bool const end_value = sizeof(g<T>(0)) == 1;
};
// Basic is_container template; specialize to derive from std::true_type for all desired container types
template<typename T> struct is_container : public ::std::integral_constant<bool,
has_const_iterator<T>::value && has_begin_end<T>::beg_value && has_begin_end<T>::end_value> { };
template<typename T, std::size_t N> struct is_container<T[N]> : public ::std::true_type { };
template<std::size_t N> struct is_container<char[N]> : public ::std::false_type { };
template <typename T> struct is_container< ::std::valarray<T>> : public ::std::true_type { };
...<snip>
这里的问题是我不清楚如何使用 SFINAE 和 enable_if 以及其余的东西来构建另一个谓词,该谓词对所有容器都评估为 true 除了 @987654336 @。
现在,这只是第一个问题。第二个问题是我的第一个代码清单中的os.write("<div class='container'>"); 行。请注意这是多么令人讨厌的不确定性。我真的希望容器的序列化例程能够报告容器的实际类型(无论是std::map 或std::forward-list 还是std::vector)。
我想知道的是是否存在一些(相当理智的)方法来使用模板实现这一点,或者我是否真的应该只使用宏来显式定义一系列模板,每个 STL 容器类型都有一个:这样我就可以轻松地为任何给定的容器构建我想要的确切类型的 HTML。
确实,使用模板枚举所有 STL 容器可以解决这两个问题。我想我会开始这样做。但我仍然想知道最初的第一个问题的答案。如何使用enable_if 排除特定类型?
【问题讨论】:
-
I'd like to treat strings as just strings, not as containers什么 -
::pretty_print::is_container<std::string>::value==true。它有一个const_iterator,它有开始和结束。不过我也有点想知道has_begin_end到底在做什么以及它为什么起作用。 -
@StevenLu,在 SFINAE 部分不是那么好,我可以告诉你
f是一个返回对 N 个字符数组的引用的函数。它在参数类型中使用 SFINAE。如果成员不存在,我想它的工作方式与在底部的调用中不考虑复杂的方法相同。我不确定它是否必须在参数中,但decltypein the return type 在 imo 中更具可读性。我想这也可以确保它也是正确类型的函数。 -
您不需要也绝对不允许在
operator<<中声明namespace std重载。