是否存在(在標準庫或Boost中)類型特徵來測試類型是否可以表示字符串?字符串的類型特徵
我偶然發現了一個問題,使用Boost.Fusion時:
auto number = fusion::make_vector(1, "one");
auto numberName = fusion::filter< char const * >(number);
assert(numberName == fusion::make_vector("one")); // fails
我希望filter
將保留「一」,但失敗了,因爲「一個」不衰減到指針(make_vector
由接受它的參數參考,所以類型是const char (&)[4]
)。因此,我需要一個特質,讓我寫的是這樣的:
auto numberName = fusion::filter_if< is_string<mpl::_> >(number);
我知道,一個char const *
和const char[N]
不一定空值終止字符串,但它仍然是得心應手能夠均勻檢測它們。該特徵還可能爲std::string
等等返回true
。
這樣的特質是否存在或我必須自己寫?
怎麼樣'static_cast(「one」)'?或者是一個將數組轉換爲指針的通用模板? –
@KerrekSB:在函數調用中封裝每個字符串似乎都是一個真正的負擔,但是可以使用函數模板來測試類型是否可以轉換爲字符指針... –
Just fyi,you're缺少assert()中的關閉karen。 – semisight