Luc*_*lle 3 c++ string traits c++11
是否存在(在标准库中或在Boost中)类型特征来测试类型是否可以表示字符串?
我在使用Boost.Fusion时偶然发现了一个问题:
auto number = fusion::make_vector( 1, "one" );
auto numberName = fusion::filter< char const * >( number );
assert( numberName == fusion::make_vector( "one" ) ); // fails
Run Code Online (Sandbox Code Playgroud)
我希望filter保留"one",但它失败了因为"one"没有衰减到指针(make_vector通过引用获取它的参数,所以类型是const char (&)[4]).因此,我需要一个特性,允许我写这样的东西:
auto numberName = fusion::filter_if< is_string< mpl::_ > >( number );
Run Code Online (Sandbox Code Playgroud)
我知道a char const *和a const char[N]不一定是以null结尾的字符串,但是能够统一检测它们仍然很方便.这种特点也可能返回true的std::string和喜欢.
这样的特质是存在还是我必须自己编写?
我尝试了实现这样的特性,但我不确定它是否真的很强大.任何输入将不胜感激.
template <typename T>
struct is_string
: public mpl::or_< // is "or_" included in the C++11 library?
std::is_same< char *, typename std::decay< T >::type >,
std::is_same< const char *, typename std::decay< T >::type >
> {};
assert ( ! is_string< int >::value );
assert ( is_string< char * >::value );
assert ( is_string< char const * >::value );
assert ( is_string< char * const >::value );
assert ( is_string< char const * const >::value );
assert ( is_string< char (&)[5] >::value );
assert ( is_string< char const (&)[5] >::value );
// We could add specializations for string classes, e.g.
template <>
struct is_string<std::string> : std::true_type {};
Run Code Online (Sandbox Code Playgroud)
这应该适用于 C++17。
#include <iostream>
#include <string>
#include <type_traits>
template<typename T>
struct is_string
: public std::disjunction<
std::is_same<char *, typename std::decay_t<T>>,
std::is_same<const char *, typename std::decay_t<T>>,
std::is_same<std::string, typename std::decay_t<T>>
> {
};
int main()
{
std::cout << std::boolalpha;
std::string str = "i am a string";
std::cout << is_string<decltype(str)>::value << std::endl; // "true"
std::cout << is_string<decltype("i am a string literal")>::value << std::endl; // "true"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2922 次 |
| 最近记录: |