I am trying to write a template is_c_str
to test if a type is a c-style string. I need this as an attempt to write a to_string function, as shown in my other question here:
Template specialization for iterators of STL containers?.
I need to tell apart c_str and other types of pointers and iterators, so that I can represent the first at the face value, and render pointers/iterators as an opaque "itor" or "ptr". The code is as follows:
#include <iostream>
template<class T>
struct is_c_str
: std::integral_constant<
bool,
!std::is_same<char *, typename std::remove_reference<typename std::remove_cv<T>::type>::type>::value
> {};
int main() {
auto sz = "Hello"; //Or: const char * sz = "Hello";
int i;
double d;
std::cout << is_c_str<decltype(sz)>::value << ", "
<< is_c_str<decltype(i)>::value << ", "
<< is_c_str<decltype(d)>::value << std::endl;
}
However, is_c_str
captures not only const char *
, but also int
and double
. The above code outputs:
1, 1, 1
(as of gcc-4.8.1).
My question is how to fix is_c_str
to properly capture c-style strings?