我正在嘗試爲std容器編寫一個流操作符,主要用於調試目的。is_container trait在std :: set SFINAE問題上失敗
我有以下代碼:
#include <type_traits>
#include <iostream>
#include <ostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
#include <set>
#include <deque>
template<typename Container>
struct is_container
{
typedef char no;
typedef long yes;
template<typename A, A, A>
struct is_of_type;
template<typename T>
static yes& is_cont(
is_of_type
<
typename T::iterator(T::*)(),
&T::begin,
&T::end
>*);
template<typename T>
static no& is_cont(...); //any other
template<typename C, bool B>
struct is_class_is_container
{
const static bool value=sizeof(is_cont<C>(nullptr))==sizeof(yes);
};
template<typename C>
struct is_class_is_container<C, false>
{
const static bool value=false;
};
const static bool value = is_class_is_container
<
Container,
std::is_class<Container>::value
>::value;
};
template<typename T>
typename std::enable_if
< is_container<T>::value, std::ostream >::type&
operator<<(std::ostream& os, const T& a)
{
os << '[';
std::copy(a.begin(), a.end(), std::ostream_iterator<typename T::value_type>(os, ", "));
os << ']';
return os;
}
我知道這是遠遠不夠完善(有建設性的意見讚賞),但我遇到的問題是,它爲載體,雙端隊列和列表的偉大工程,但未能匹配集合,我不知道爲什麼因爲集合仍然有迭代器接口的開始和結束。
謝謝。
編輯:於 G ++(GCC)4.6.2 2012012 鐺版本測試3.0
EDIT2:我得到了它使用decltype排序的工作不過是次優,因爲現在我不能斷言它我期望的(返回一個迭代器)。
我並不完全知道該設置是什麼返回首先,也許如果有人有調試TMP的方式,這將是一件好事。
我在[漂亮的打印]某種形式的'is_container'特質(http://stackoverflow.com/questions/4850473/pretty-print-c -stl-containers),如果你有興趣。 – 2012-02-11 17:16:56
@KerrekSB謝謝,我會仔細考慮一下,但是,因爲這真的是對我進行這樣的測試,所以我希望能夠做到這一點,並嘗試使其發揮作用。 – 111111 2012-02-11 17:21:20
這看起來不必要。試試這個:http://ideone.com/gBx6P – 2012-02-11 18:17:54